【问题标题】:can't get a label to update from wpf in another class无法从另一个类中的 wpf 获取要更新的标签
【发布时间】:2014-02-14 06:30:33
【问题描述】:

我正在制作一个基本程序,当用户在文本框中键入时,标签会更新。我正在尝试使用数据绑定和 INotifyPropertyChanged 来解决这个问题,所以我不想要任何解决方法。我使用了 2 个按钮,所以我实际上可以查看它们是否更新。这是我的主要课程

namespace TestStringChangeFromAnotherClass

public partial class MainWindow : Window
{

    textClass someTextClass = new textClass();
    public MainWindow()
    {

        InitializeComponent();

    }

    public string someString1;
    public string someString2;

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        someTextClass.Text1 = tbx1.Text;
    }

    private void btn2_Click(object sender, RoutedEventArgs e)
    {
        someTextClass.Text2 = tbx1.Text;
    }
}

这是它的 wpf

<Window x:Class="TestStringChangeFromAnotherClass.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="MainWindow" Height="350" Width="525">

    <Grid>
    <Button x:Name="btn1" Content="Button" HorizontalAlignment="Left" Height="36" Margin="29,246,0,0" VerticalAlignment="Top" Width="108" Click="btn1_Click"/>
    <Button x:Name="btn2" Content="Button" HorizontalAlignment="Left" Height="36" Margin="227,246,0,0" VerticalAlignment="Top" Width="124" Click="btn2_Click"/>
    <Label x:Name="lbl1" Content="{Binding textClass.Text1}" HorizontalAlignment="Left" Height="37" Margin="74,32,0,0" VerticalAlignment="Top" Width="153"/>
    <Label x:Name="lbl2" Content="{Binding textClass.Text2, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="38" Margin="74,90,0,0" VerticalAlignment="Top" Width="153"/>
    <TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="37" Margin="290,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="190"/>

</Grid>

如您所见,我已尝试使用 UpdateSourceTrigger。我还尝试使用“someTestClass.Text1”而不是 textClass.Test1,因为这就是我在 MainWindow 中定义它的方式。这是我的文本类

namespace TestStringChangeFromAnotherClass
public class textClass:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string text1;
    public string Text1
    {
        get { return text1; }
        set
        {
            text1 = value;
            NotifyPropertyChanged("Text1");
        }
    }

    private string text2;
    public string Text2
    {
        get { return text2; }
        set
        {
            text2 = value;
            NotifyPropertyChanged("Text2");
        }
    }

    protected void NotifyPropertyChanged(string info)
    {

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

我不知道如何让 wpf 在单独的类中查找 Test1 或 Test2 字符串并在字符串更改时更新它们。我感觉问题出在 DataContext 中,但我想不通。我也宁愿不在 c# 中使用 DataContext,只在 WPF 中使用

更新: 当我调试它时,当它到达 NotifyPropertyChanged 时,PropertyChanged 被评估为空。会不会是这个问题?

【问题讨论】:

  • 您是否也尝试过在 Mainwindow 中实现 INotifyPropertyChanged?因为这是您的数据上下文。
  • 如果我在 MainWindow 中添加 INotifyPropertyChanged,我会收到此错误 -'TestStringChangeFromAnotherClass.MainWindow' 没有实现接口成员 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged'
  • 也许,我需要做的是尝试让 UpdateSourceTrigger 查看 textClass 类中的 PropertyChanged 项?

标签: c# wpf properties inotifypropertychanged


【解决方案1】:

您将DataContext 绑定到您的Window,据我所知,它没有textClass 属性。它具有someTextClass 类型的textClass 字段。为了让您的代码正常工作,您可以将 someTextClass 更改为公共属性:

public textClass someTextClass { get; private set; }

在构造函数中初始化它:

public MainWindow()
{
    someTextClass = new textClass();
    InitializeComponent();

}

然后将绑定更改为指向someTextClass 属性

<Label x:Name="lbl1" Content="{Binding someTextClass.Text1}" .../>
<Label x:Name="lbl2" Content="{Binding someTextClass.Text2}" .../>

【讨论】:

  • 将初始化移入构造函数并更改绑定属性,也将所有属性更改为public,不起作用
  • 你的代码和我的例子一模一样吗?在发布答案之前我已经尝试过这段代码,它运行良好
  • 现在可以使用了。诡异的。我在 InitializeComponent 之后对其进行了初始化。我必须在 InitializeComponent 之前对其进行初始化吗?这是为什么?什么是“得到;私人集合;”做什么?
  • 这是正确的,这是因为 someTextClass 属性在更改时不会通知,因此它必须在创建绑定之前可用。 get; private set; 表示 getter 与属性具有相同的可见性,因此是公共的,而 setter 是私有的,因此只能在 MainWindow 类中设置
  • 有更合适的方法吗?谢谢一百万,另外,我在过去的 5 个小时里一直在做这个!
【解决方案2】:

您将绑定到MainWindow 类本身作为您的DataContext,并尝试访问名为someTextClass 的属性,该属性具有您要绑定到的属性。

你遇到了两个问题:

1) 您的 XAML 试图通过其类型而不是名称来引用所需对象。不去上班。您的绑定表达式应类似于 {Binding someTextClass.Text1}(注意路径表达式第一部分的区别)。

2) 你只能绑定到公共的东西。您的字段未定义为公共字段,因此是私有的。即使 XAML 在逻辑上应该“能够看到”该属性,因为它是同一个类,但 DataBinding 仅适用于公共属性。

3) 编辑:您还必须将其设为属性。 WPF 不会绑定到字段。

一般来说,使用Snoop 将有助于诊断静默绑定错误。

【讨论】:

  • 1.我将其更改为绑定,但不起作用。 2. 我把所有私人的都改成了公共的。那也没有用。 3. 不知道你的意思是什么?
  • 不要使用textClass someTextClass = new textClass();,而是使用public textClass someTextClass {get; private set;},并在您的构造函数中添加someTextClass = new textClass(); What is the difference between a field and a property in C#? 可能有助于进一步解释有关字段和属性的信息。
猜你喜欢
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 2020-10-15
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多