WPF TextBlock绑定到字符串

时间:2015-03-26 22:29:18

标签: c# wpf xaml

我想将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个多小时,仍然无法让它工作。我错过了什么?

3 个答案:

答案 0 :(得分:6)

如果您需要更新绑定,则属性Joke必须是DependencyPropertyWindows必须实现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 propertyTextBlock确实绑定到该属性(如果您已正确设置data context):

Text="{Binding Joke}"

然而,缺少的是绑定不可能知道属性值已经改变。您需要发布有关财产更改的通知

WPF绑定可以识别两种方法:

答案 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