[英]PropertyChanged member for INotifyPropertyChanged is always null
I'm trying to bind a text block to my variable slctItem
. 我正在尝试将文本块绑定到我的变量
slctItem
。 I can see it contain the necessary data I need however my window does not show the data I'm expecting. 我可以看到它包含了我需要的必要数据,但是我的窗口没有显示我期望的数据。 Here is the code behind for my control.
这是我控制的背后代码。 This control is used by a pop up window which will display the values of the control.
该控件由弹出窗口使用,该窗口将显示控件的值。
When walking the code I see that handler returns null every time in the OnPropertyChanged()
method. 遍历代码时,我看到处理程序每次在
OnPropertyChanged()
方法中都返回null。 Why? 为什么? I must be doing something wrong here.
我在这里一定做错了。 Again
slcItem
does contain the data I'm wanting to use. 同样,
slcItem
确实包含我要使用的数据。 The OnPropertyChanged()
method also fires it just contains null for handler. OnPropertyChanged()
方法也会触发它只包含处理程序的null。
public partial class MetaData : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _slctItem;
public MetaData()
{
InitializeComponent();
}
public string slctItem
{
get
{
return _slctItem;
}
set
{
_slctItem = value;
OnPropertyChanged("slctItem");
}
}
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
internal void Refresh()
{
try
{
// If DataContext is Null or a detached DataRow, disable the view
if (DataContext != null && (DataContext is DataRow && ((DataRow)DataContext).RowState != System.Data.DataRowState.Detached))
{
if (DataContext is "Something Here")
{
slctItem = (("Something Here")this.DataContext).NAME;
}
}
}
catch (Exception e)
{
throw new Exception("MetaData -> Refresh(): " + e.Message);
}
}
Here is the XAML code for my control. 这是我的控件的XAML代码。 Here I'm trying to bind to
slctItem
在这里我试图绑定到
slctItem
<TextBox Grid.Column="2" Grid.Row="0" Text="{Binding Path=slctItem, Mode=OneWay, Converter={StaticResource myFirstCharToUpperConverter}}" Width="150" Height="25" HorizontalAlignment="Left" />
You need to set the DataContext
to yourself: 您需要为自己设置
DataContext
:
public MetaData()
{
InitializeComponent();
this.DataContext = this;
}
This will allow the binding to find the appropriate property. 这将使绑定找到合适的属性。 Right now, if you look at the
Debug Output
in the Output Window at runtime, you should see binding errors since the data context is unset. 现在,如果在运行时在“输出”窗口中查看“
Debug Output
,则应该看到绑定错误,因为未设置数据上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.