问题描述
你好社区!
学习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!
推荐答案
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.
这篇关于关于INotifyPropertyChanged的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!