无法从另一个类的wpf获取标签

时间:2014-01-23 15:38:46

标签: c# wpf properties inotifypropertychanged

我正在制作一个基本程序,当用户键入文本框时,标签会更新。我正在尝试使用数据绑定和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中定义它的方式。这是我的textClass

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被评估为null。这可能是问题吗?

2 个答案:

答案 0 :(得分: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}" .../> 属性

{{1}}

答案 1 :(得分:0)

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

您遇到两个问题:

1)你的XAML试图通过它的类型引用所需的对象,而不是它的名字。不会工作。您的绑定表达式应该看起来像{Binding someTextClass.Text1}(注意路径表达式第一部分的差异)。

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

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

通常,使用Snoop将有助于诊断静默绑定错误。