关于INotifyPropertyChanged的问题 [英] Question about INotifyPropertyChanged

查看:60
本文介绍了关于INotifyPropertyChanged的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好社区!
学习WPF并非那么容易:P

我有以下问题:

我有一个Textbound,其中DataContext在backcode中绑定到"DVD"实例:

Hello community!
Learning WPF isn''t that easy :P

I have the following problem:

I have a Textbound which DataContext is bound to a "DVD"-instance in backcode:

DvdNameTextBox.DataContext = myDvd;



但是,如果我像以下那样更换DVD:



But if I change my DVD like the following:

myDvd = new Dvd("From outer space", 1978);



我的绑定没有更新.

现在,我尝试将myDvd放入名为MainWindowData的类中,该类使INotifyPropertyChanged并尝试将DataContext设置为



my Binding doesn''t get updated.

Now i tried to put myDvd into a class called MainWindowData, which i made INotifyPropertyChanged and tried to set the DataContext to

myData.myDvd


...仍然没有更新.

现在我发现,如果我将DataContext设置为


...Still no update.

Now i found out if i set the DataContext to

myData

并将Binding设置为

and the Binding to

Text="myDvd.Name"

,则文本将被更新,因为我的PropertyChangedEventHandler不再为null(我认为是因为我将myData用作DataContext)

会有一种更简单的方法来实现数据绑定吗?我只想获取我的DVD名称,并且必须将Backcode更改为DVD实例.

感谢您的帮助,对于冗长的问题深表歉意:)

--------------------------------
--------------------------------
--------------------------------

改善问题:

我在MainWindowData中:

私人Mix selectedDVD = null;

that the text gets updated because my PropertyChangedEventHandler isn''t null anymore (I think because I used myData as DataContext)

Would have been there an more easy way to achieve data binding? I just want to get my DVDs name and have to change to DVD instance in backcode.

Thanks for your help and sorry for the long question :)

--------------------------------
--------------------------------
--------------------------------

IMPROVE QUESTION:

I have in MainWindowData:

private Mix selectedDVD = null;

public Mix SelectedDVD
{
  get { return selectedDVD; }
  set
  {
    selectedDVD = value;
    NotifyPropertyChanged("SelectedDVD");
  }
}



在WindowLoaded上执行:



On WindowLoaded I do:

MixNameTextBox.DataContext = Data.SelectedDVD;



我的XAML如下所示:



My XAML looks the following:

<TextBlock Name="DVDNameTextBox" VerticalAlignment="Center" Text="{Binding Name, Converter={StaticResource ReplaceNullStringConverter},ConverterParameter='No DVD selected'}" ></TextBlock>



然后在ListBox.SelectionChanged(用于选择DVD的ListBox)上:



And on ListBox.SelectionChanged (the ListBox to select the DVD):

Data.SelectedDVD = (DVD)e.AddedItems[0];



在这一行上存在问题:程序不介意源已更改.
在MyWindowData和/或DVD上实现INotifyPropertyChange并不会改变任何内容.

为什么我的文本框没有意识到它有一个新的数据源?

谢谢!



And on this line there is the problem: The program doesn''t mind that the source was changed.
It doesn''t change anything to implement INotifyPropertyChange on MyWindowData and/or on DVD.

Why doesn''t my Textbox realise that it has a new DataSource?

Thanks!

推荐答案


问题是您的SelectedDVD 不是dependency property.绑定到Name属性时,不会通知您其值更改.因此,您应该将Name 设置为依赖项属性,或者将整个SelectedDVD 对象设置为依赖项属性.

这是一个有效的示例,我将整个SelectedDVD 更改为依赖项属性(也将其名称更改为Mix):
Hi,
The problem is that your SelectedDVD is not a dependency property. When you bind to the Name property you are not notified of its value changes. So you should either make Name into a Dependency Property or The whole SelectedDVD object into a dependency property.

Here is a working example that I have changed the whole SelectedDVD into a dependency property (Also I have changed its name to Mix) :
public partial class CPTest1 : Window
{
    public static readonly DependencyProperty MixProperty =
        DependencyProperty.Register("Mix", typeof (Mix), typeof (CPTest1), new PropertyMetadata(default(Mix)));

    public Mix Mix
    {
        get { return (Mix) GetValue(MixProperty); }
        set { SetValue(MixProperty, value); }
    }

    public CPTest1()
    {
        InitializeComponent();

        Mix = new Mix("Film 1", 2009);

        DVDNameTextBox.DataContext = this;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Mix = new Mix("Film 2", 2011);
    }

}

public class Mix
{
    public Mix(string name, int year)
    {
        Name = name;
        Year = year;
    }

    public string Name { get; set; }
    public int Year { get; set; }
}






和XAML部分:






And the XAML part :

<TextBlock Name="DVDNameTextBox" VerticalAlignment="Center" Text="{Binding Path=Mix.Name}" ></TextBlock>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="178,42,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />




要创建依赖项属性,可以使用Intelisense获得帮助.

祝您好运.




For creating dependency properties you can use Intelisense for getting assistance.

Good Luck.


您似乎想做的是双向绑定.这是一个代码项目文章,应该可以帮助您解决问题:

简单的WPF数据绑定(带有一些其他WPF优点) [ ^ ]
It looks like what you want to do is two-way binding. Here is a Code Project article that should help clear up your questions:

Simple WPF databinding (with some additional WPF goodies)[^]


DataContext应该设置为MyDvd

用于分配Text ="Name"

< textbox datacontext ="MyDvd" text ="{绑定名称,模式= TwoWay}">

这可以100%
The DataContext should be set to MyDvd

For assigning Text="Name"

<textbox datacontext="MyDvd" text="{Binding Name, Mode = TwoWay}">

This works 100%


这篇关于关于INotifyPropertyChanged的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆