WPF TextBlock 绑定到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29290057/
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
WPF TextBlock Binding to a String
提问by Bruno
I want to bind a TextBlock to a string which takes its value from a txt file. The string is correctly filled but its contents are not displayed.
我想将 TextBlock 绑定到一个字符串,该字符串从 txt 文件中获取其值。字符串已正确填充,但未显示其内容。
Class file:
类文件:
public partial class JokesMessageBox : Window
{
public JokesMessageBox()
{
InitializeComponent();
}
public string Joke { get; set; }
public string path = "data/jokes.txt";
public void ReadFile(string path)
{
Joke = File.ReadAllText(path);
}
}
XAML:
XAML:
<TextBlock HorizontalAlignment="Left" Margin="22,10,0,0"
TextWrapping="Wrap" Text="{Binding Joke}" VerticalAlignment="Top"
Height="60" Width="309"/>
EDIT:
编辑:
In the MainWindow class:
在 MainWindow 类中:
private void btnJokesFirstScreen_Click_1(object sender, RoutedEventArgs e)
{
JokesMessageBox jkb = new JokesMessageBox();
jkb.Show();
jkb.ReadFile("data/jokes.txt");
}
I spent 3+ hours on google, youtube, MSDN, StackOverflow and still can't get it working. What am I missing?
我在 google、youtube、MSDN、StackOverflow 上花了 3 个多小时,但仍然无法正常工作。我错过了什么?
回答by dbvega
If the you need to update the binding, the property Jokemust be a DependencyPropertyor the Windowsmust implement INotifyPropertyChangedinterface.
如果您需要更新绑定,则该属性Joke必须是一个DependencyProperty或Windows必须实现的INotifyPropertyChanged接口。
On the view, the binding needs to know Source.
在视图上,绑定需要知道Source.
Example #1 (Using DependencyProperty):
示例 #1(使用DependencyProperty):
public partial class JokesMessageBox : Window
{
public JokesMessageBox()
{
InitializeComponent();
ReadFile(Path); //example call
}
public string Joke
{
get { return (string)GetValue(JokeProperty); }
set { SetValue(JokeProperty, value); }
}
public static readonly DependencyProperty JokeProperty =
DependencyProperty.Register("Joke", typeof(string), typeof(JokesMessageBox), new PropertyMetadata(null));
public const string Path = "data/jokes.txt";
public void ReadFile(string path)
{
Joke = File.ReadAllText(path);
}
}
Example #2 (Using INotifyPropertyChangedinterface):
示例#2(使用INotifyPropertyChanged接口):
public partial class JokesMessageBox : Window, INotifyPropertyChanged
{
public JokesMessageBox()
{
InitializeComponent();
ReadFile(Path); //example call
}
private string _joke;
public string Joke
{
get { return _joke; }
set
{
if (string.Equals(value, _joke))
return;
_joke = value;
OnPropertyChanged("Joke");
}
}
public const string Path = "data/jokes.txt";
public void ReadFile(string path)
{
Joke = File.ReadAllText(path);
}
//INotifyPropertyChanged members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
And the view (XAML partial):
和视图(XAML 部分):
...
<TextBlock HorizontalAlignment="Left" Margin="22,10,0,0"
TextWrapping="Wrap"
Text="{Binding Joke,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
VerticalAlignment="Top"
Height="60" Width="309"/>
...
I hope it helps.
我希望它有帮助。
回答by Ivan Vasiljevic
Your class is not implementing INotifyPropertyChanged interface. So when you change property Joke TextBlock is not updated. I would do something like this:
您的课程没有实现 INotifyPropertyChanged 接口。因此,当您更改属性 Joke TextBlock 不会更新。我会做这样的事情:
public partial class JokesMessageBox : Window, INotifyPropertyChanged
{
public JokesMessageBox()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
public string Joke { get; set; }
public string path = "data/jokes.txt";
public void ReadFile(string path)
{
Joke = File.ReadAllText(path);
OnPropertyChanged("Joke");
}
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I would also suggest you to read about MVVM patern.
我还建议您阅读有关MVVM 模式的信息。
回答by O. R. Mapper
When you read the contents of the file, you assign the read string to your Jokeproperty:
当您读取文件的内容时,您将读取字符串分配给您的Joke属性:
Joke = File.ReadAllText(path);
The Textpropertyof the TextBlockis indeed bound to that property (ifyou have properly set the data context):
该Text物业的TextBlock确实被绑定到该属性(如果你已经设定了正确的数据上下文):
Text="{Binding Joke}"
However, what is missing is that the binding cannot possibly have any idea that the property value has changed. You need to issue a notificationabout the property change.
但是,缺少的是绑定不可能知道属性值已更改。您需要发出有关属性更改的通知。
There are two ways to do this that will be recognized by WPF bindings:
有两种方法可以通过 WPF 绑定识别执行此操作:
- You declare your
Jokeproperty as a dependency property. This is based on some WPF infrastructure that automatically issues the change notifications. - You have your class implement the
INotifyPropertyChangedinterface. Here, you have to implement a simple interface with aPropertyChangedevent, which you have to fire in your property setter while passing the name of the property as a string.
- 您将您的
Joke属性声明为依赖属性。这是基于一些自动发出更改通知的 WPF 基础结构。 - 你让你的类实现
INotifyPropertyChanged接口。在这里,您必须使用PropertyChangedevent实现一个简单的接口,您必须在属性设置器中触发它,同时将属性名称作为字符串传递。

