我想将TextBlock绑定到一个字符串,该字符串从txt文件获取其值。字符串已正确填充,但其内容未显示。
类文件:
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:
<TextBlock HorizontalAlignment="Left" Margin="22,10,0,0"
TextWrapping="Wrap" Text="{Binding Joke}" VerticalAlignment="Top"
Height="60" Width="309"/>
编辑:
在MainWindow类中:
private void btnJokesFirstScreen_Click_1(object sender, RoutedEventArgs e)
{
JokesMessageBox jkb = new JokesMessageBox();
jkb.Show();
jkb.ReadFile("data/jokes.txt");
}
我在google,youtube,MSDN,StackOverflow上花了3个多小时,仍然无法让它工作。我错过了什么?
答案 0 :(得分:6)
如果您需要更新绑定,则属性Joke
必须是DependencyProperty
或Windows
必须实现INotifyPropertyChanged
接口。
在视图中,绑定需要知道Source
。
示例#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);
}
}
示例#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));
}
}
视图(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"/>
...
我希望它有所帮助。
答案 1 :(得分:0)
当您阅读文件的内容时,您将读取的字符串分配给Joke
属性:
Joke = File.ReadAllText(path);
Text
property的TextBlock
确实绑定到该属性(如果您已正确设置data context):
Text="{Binding Joke}"
然而,缺少的是绑定不可能知道属性值已经改变。您需要发布有关财产更改的通知。
WPF绑定可以识别两种方法:
Joke
属性声明为dependency property。这基于一些自动发布更改通知的WPF基础结构。INotifyPropertyChanged
interface。在这里,您必须实现一个带有PropertyChanged
event的简单接口,您必须在属性设置器中触发该接口,同时将该属性的名称作为字符串传递。答案 2 :(得分:0)
您的类未实现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));
}
}
}
我还建议您阅读MVVM patern。