我试图将文本块绑定到我的变量slctItem
。我可以看到它包含我需要的必要数据,但是我的窗口没有显示我期待的数据。这里是我的控制背后的代码。该控件由弹出窗口使用,该窗口将显示控件的值。PropertyChanged成员INotifyPropertyChanged始终为空
行走代码时,我看到该处理程序每次都在OnPropertyChanged()
方法中返回null。为什么?我一定在这里做错了事。 slcItem
也包含我想要使用的数据。 OnPropertyChanged()
方法也触发它只包含处理程序的空值。
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);
}
}
这是我的控件的XAML代码。在这里,我试图绑定到slctItem
<TextBox Grid.Column="2" Grid.Row="0" Text="{Binding Path=slctItem, Mode=OneWay, Converter={StaticResource myFirstCharToUpperConverter}}" Width="150" Height="25" HorizontalAlignment="Left" />
它看起来像你从来没有设置'DataContext'属性。那个代码是否丢失或者你忘了写它?没有它,事件处理程序不会设置。 – BradleyDotNET