TextBlock中的绑定在WPF中不起作用 [英] Binding in TextBlock doesn't work in WPF
问题描述
我想动态地更改我的Class中的 TextBlock
文本。
XAML代码:
< TextBlock Name =Footer_textText =/>
C#:
string footerMainMenuText =设置;
Binding set = new Binding();
set.Mode = BindingMode.OneWay;
set.Source = footerMainMenuText;
Footer_text.DataContext = footerMainMenuText;
Footer_text.SetBinding(TextBlock.TextProperty,set);
我检查了最后一行,而$ code> Footer_text.Text 设置正确。 ( Footer_text.Text =设置
),但在我的应用程序中 TextBlock
不显示设置。这里有什么问题?
如果你有约束力 - 为什么不只是在XAML中做?看看你的代码,这是没有意义的 - 你可能只是去
Footer_text.Text =设置;你应该理想地在XAML中做到这一点,或至少提供一些绑定到的东西。
< TextBlock Text ={Binding SomeProperty}/>
我不知道为什么你会绑定一个字符串在自己的东西...你有没有需要绑定到文本属性的对象?
还使用
Binding()
这是做什么的?空白路径?不知道绑定目标是什么吗?你尝试过
Binding()
代替?
编辑:
此外,您的绑定不更新控件的原因可能是因为您没有绑定到实现INotifyPropertyChanged或类似接口的对象。控件需要知道值何时发生变化,所以我想象的是,绑定到'string'的时候不会给TextBlock提供正确的通知。
编辑2 :
这是绑定工作的一个快速例子:
我的窗口类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 =MainWindowHeight =350Width = 525>
< Grid>
< StackPanel>
< TextBlock x:Name =txtNameText ={Binding Name}>< / TextBlock>
< Button Click =Button_Click>点击我1< / Button>
< Button Click =Button_Click_1>点击我2< / Button>
< / StackPanel>
< / Grid>
< / Window>
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) p>
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,但它更新文本框。
I want to dynamically change TextBlock
text in my Class.
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);
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?
解决方案 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";
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?
Also using
Binding("")
What does that do? A blank path? Not sure what the binding target would be there... have you tried
Binding()
instead?
Edit:
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
Edit 2:
Here is a quick example of binding working:
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>
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";
}
}
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));
}
}
Clicking the buttons changes SomeObject.Name, but it updates the textbox.
这篇关于TextBlock中的绑定在WPF中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!