2014-07-10 52 views
1

我试图将文本块绑定到我的变量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" /> 
+0

它看起来像你从来没有设置'DataContext'属性。那个代码是否丢失或者你忘了写它?没有它,事件处理程序不会设置。 – BradleyDotNET

回答

1

您需要的DataContext设置自己:

public MetaData() 
{ 
    InitializeComponent(); 
    this.DataContext = this; 
} 

这将允许结合查找相应的财产。现在,如果您在运行时查看输出窗口中的Debug Output,则应该看到绑定错误,因为数据上下文未设置。

+0

所以我不能这样做,因为我像这样在XAML方面设置了DataContext。如果我实施您的解决方案,则不再显示CREATED_BY。 '' 但是你的DataContext是有道理的。我只是不确定在SlimItem的XAML端设置什么? – GET1NE

+0

@ GET1NE您可以使用RelativeSource绑定绑定到self。 –

+0

所以这工作。谢谢您的帮助! '' – GET1NE