将 C# 中的变量数据绑定到 WPF 应用程序中的文本块不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24527758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:03:00  来源:igfitidea点击:

Databinding a variable in C# to a textblock in a WPF application not working

c#wpfxaml

提问by ocajian

XAML, C# novice and am struggling to databind a variable defined in my code behind to a textblock defined in XAML. But I get not result.

XAML、C# 新手,并且正在努力将我的代码中定义的变量数据绑定到 XAML 中定义的文本块。但我没有得到结果。

Here is my XAML

这是我的 XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Loaded="Window_Loaded_1">
<Grid>
    <TextBlock Name="totalRecording">
                        <Run Text="44 /"/>
                        <Run Text="{Binding Source=listlength, Path=totalRecording}"/>
    </TextBlock>
</Grid>

Here is my code behind

这是我的代码

namespace WpfApplication1
{

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        var listlength = 100;
    }
}
}

For now I have just set the variable to a static number for the purposes of illustrating my problem but this variable will be obtained from a list Count value.

现在,为了说明我的问题,我只是将变量设置为静态数字,但该变量将从列表 Count 值中获取。

回答by Heena Patil

For binding you need to use Propertyonly .you cannot use varibale for binding.

对于绑定,您只需要使用Property 。您不能使用变量进行绑定。

To create property I create a class

要创建属性,我创建了一个类

  public class TextboxText
{
    public string textdata { get; set; }

}

And set datacontext to textblock so that I can use this property for binding

并将 datacontext 设置为 textblock 以便我可以使用此属性进行绑定

InitializeComponent();
totalRecording.DataContext = new TextboxText() { textdata = "100" };

in xaml

在 xml 中

<Grid Height="300" Width="400" Background="Red">
    <TextBlock Name="totalRecording">
       <Run Text="44 /"/>
       <Run Text="{Binding textdata}"/>
    </TextBlock>
</Grid

回答by Linkinbikzit

If you want to updatethe Binding, you should use a DependencyProperty.

如果要更新绑定,则应使用DependencyProperty

First you have to create the propertyand a public stringlike this:

首先,您必须像这样创建属性公共字符串

public static readonly DependencyProperty ListLengthProperty =
        DependencyProperty.Register("ListLength", typeof(string), typeof(Window), new PropertyMetadata(null));

    public string ListLength
    {
        get { return (string)GetValue(ListLengthProperty); }
        set { SetValue(ListLengthProperty, value); }
    }

Here is the XAMLfile, you need to set a namefor the window:

这是XAML文件,您需要为窗口设置一个名称

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="CurrentWindow"
    Title="MainWindow" Height="350" Width="525"
    Loaded="Window_Loaded_1">
<Grid>
    <TextBlock Name="totalRecording">
                    <Run Text="44 /"/>
                    <Run Text="{Binding ListLength, ElementName=CurrentWindow}"/>
    </TextBlock>
</Grid>

Now you can always update the Binding by setting the ListLengthlike this:

现在,您始终可以通过像这样设置ListLength来更新绑定:

ListLength = "100";

回答by Sajeetharan

Just use TextBlock,

只需使用TextBlock,

     <Grid Name="myGrid" Height="437.274">
      <TextBox Text="{Binding Path=listlength}"/>
    </Grid>

Declare the variable and Implement InotifyPropertyChanged

声明变量并实现 InotifyPropertyChanged

    partial class Window1 : Window, INotifyPropertyChanged
    {
      public event PropertyChangedEventHandler PropertyChanged;

      private string _listlength;

      public string Listlength
      {
        get { return _listlength; }
        set
        {
          if (value != _listlength)
          {
             _listlength = value;
             OnPropertyChanged("Listlength");
          }
        }
      }

}