在TextBlock的绑定不工作WPF [英] Binding in TextBlock doesn't work in WPF
问题描述
我要改变我的类TextBlock的动态文本。我做了所有的事情,但它不正常工作。
XAML的代码:
i want to change dynamically text of a TextBlock in my Class. i did all thing but it doesnt work properly. XAML-Code:
<TextBlock Name="Footer_text" Text=""/>
在C#中:
string footerMainMenuText = "Setting";
Binding set = new Binding("");
set.Mode = BindingMode.OneWay;
set.Source = footerMainMenuText;
Footer_text.DataContext = footerMainMenuText;
Footer_text.SetBinding(TextBlock.TextProperty, set);
我检查最后一行,并Footer_text.Text是否设置正确。(Footer_text.Text =设置),但TextBlock的在我的应用程序犯规秀设置。这里有什么问题吗?谢谢
i checked last line, and the Footer_text.Text is set correctly.( Footer_text.Text="Setting"), but TextBlock in my application doesnt show "Setting". what is the problem here? thanks
推荐答案
如果你要绑定 - 为什么不只是做在XAML中呢?看你的代码,这是一种毫无意义的 - 你可能也只是去
If you are binding - why not just do it in XAML instead? Looking at your code it's kind of pointless - you might as well just go
Footer_text.Text = "Setting";
您最好应做到在XAML,或至少提供的东西为它绑定到
You should ideally do it in XAML or at least provide something for it to bind to
<TextBlock Text="{Binding SomeProperty}" />
我不知道为什么你会在它自己的绑定'字符串'到什么...你有哪些需要绑定到文本属性的对象?
I'm not sure why you would bind a 'string' on it's own to anything...do you have an object which you need to bind to the text property?
同时使用
Binding("")
这是什么呢?一个空白的路径?不知道是什么的约束性目标会在那里......你试过
What does that do? A blank path? Not sure what the binding target would be there... have you tried
Binding()
而不是
编辑:
此外,为什么你的绑定更新不及时的控制的原因,可能是因为你还没有绑定到哪个实现INotifyPropertyChanged或类似的接口的对象。这些控件需要知道什么时候改变过,所以我会想象,结合串不给TextBlock的正确通知时,它改变了
Also the reason why your binding is not updating the control, is probably because you haven't bound to an object which implements INotifyPropertyChanged or a similar interface. The controls need to know when values have changed, so I'd imagine that binding to 'string' isn't giving the TextBlock the proper notification when it changes
编辑2
下面是结合工作的一个简单的例子:
Here is a quick example of binding working:
我的窗口类Window.cs:
My window class Window.cs:
<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">
<Grid>
<StackPanel>
<TextBlock x:Name="txtName" Text="{Binding Name}"></TextBlock>
<Button Click="Button_Click">Click me 1</Button>
<Button Click="Button_Click_1">Click me 2</Button>
</StackPanel>
</Grid>
</Window>
在Window.xaml.cs后面的代码
The code behind in Window.xaml.cs
public partial class MainWindow : Window
{
SomeObjectClass obj = new SomeObjectClass();
public MainWindow()
{
InitializeComponent();
txtName.DataContext = obj;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
obj.Name = "Hello World";
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
obj.Name = "Goobye World";
}
}
对象绑定到(与INotifyPropertyChanged的)
The object to bind to (with INotifyPropertyChanged)
class SomeObjectClass : INotifyPropertyChanged
{
private string _name = "hello";
public string Name
{
get
{
return _name;
}
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
点击按钮改变SomeObject.Name,但它更新文本框。
Clicking the buttons changes SomeObject.Name, but it updates the textbox.
这篇关于在TextBlock的绑定不工作WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!