c# - WPF TextBlock 绑定(bind)到字符串

标签 c# wpf xaml

我想将 TextBlock 绑定(bind)到从 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 个多小时,但仍然无法正常工作。我错过了什么?

最佳答案

如果您需要更新绑定(bind),属性 Joke 必须是 DependencyPropertyWindows 必须实现 INotifyPropertyChanged 接口(interface)。

在 View 上,绑定(bind)需要知道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接口(interface)):

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));
    }
}

以及 View (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"/>
...

希望对你有帮助。

关于c# - WPF TextBlock 绑定(bind)到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29290057/

相关文章:

c# - 如何以编程方式在默认 PDF 阅读器中打开 PDF?

c# - 使用超过 2^31 个元素的虚拟化集合进行 UI 虚拟化

WPF ViewModel 命令可以执行问题

c# - 具有不同组合的 XAML 网格布局

c# - 将 int 视为字符串的 sql 查询 - 问题?

c# - 如何消除 foreach 循环中的虚假/多余空格?

c# - 使用 QnAMaker 的 Azure 聊天机器人 - 应根据当前用户的时区向用户打招呼

C#打开文件对话框;指定名称和扩展名?

c# - UWP 应用程序在从包部署后运行 - 崩溃

c# - Xamarin.Forms 空白应用程序 (PCL) 在 Android 上编译错误