2011-02-07 85 views
0

我在我的應用程序中有一個自定義數據網格,當它的源代碼爲空時,它應該顯示一條消息,說「沒有結果」,並且當源不是空的時候它應該顯示源數據並且不應該顯示消息。MVVM RaisePropertyChanged

我的自定義數據網格是:

namespace GenericControls 
{ 
    [TemplatePart(Name = "EmptyDataGridTextBlock", Type = typeof(TextBlock))] 
    public class CustomDataGrid : DataGrid 
    { 
     private TextBlock _txtEmptyDataGrid = null; 

     public static readonly DependencyProperty IsEmptyDataGridProperty = DependencyProperty.Register("IsEmptyDataGrid", typeof(bool), typeof(CustomDataGrid), null); 
     public static readonly DependencyProperty EmptyDataGridTextProperty = DependencyProperty.Register("EmptyDataGridText", typeof(string), typeof(CustomDataGrid), null); 

     public CustomDataGrid() 
     { 
      IsEmptyDataGrid = true; 
     } 

     public bool IsEmptyDataGrid 
     { 
      get 
      { 
       return (bool)base.GetValue(IsEmptyDataGridProperty); 
      } 
      set 
      { 
       base.SetValue(IsEmptyDataGridProperty, value); 
       if(_txtEmptyDataGrid != null) 
       { 
        _txtEmptyDataGrid.Visibility = IsEmptyDataGrid ? Visibility.Visible : Visibility.Collapsed; 
       }     
      } 
     } 

     public string EmptyDataGridText 
     { 
      get 
      { 
       if (_txtEmptyDataGrid != null) 
       { 
        return _txtEmptyDataGrid.Text; 
       } 

       return (string)base.GetValue(EmptyDataGridTextProperty); 
      } 
      set 
      { 
       if (_txtEmptyDataGrid != null) 
       { 
        _txtEmptyDataGrid.Text = value; 
       } 
       base.SetValue(EmptyDataGridTextProperty, value);     
      } 
     } 

     public override void OnApplyTemplate() 
     { 
      base.OnApplyTemplate(); 

      _txtEmptyDataGrid = GetTemplateChild("EmptyDataGridTextBlock") as TextBlock; 
      if (_txtEmptyDataGrid != null) 
      { 
       _txtEmptyDataGrid.Text = EmptyDataGridText; 
       _txtEmptyDataGrid.Visibility = IsEmptyDataGrid ? Visibility.Visible : Visibility.Collapsed; 
      } 
     } 
    } 
} 

我的XAML如下:我:CustomDataGrid HEIGHT = 「180」 NAME = 「dgChildren」 的SelectedItem = 「{結合SelectedChild,模式=雙向}」的ItemsSource = 「{結合兒童}」 IsEmptyDataGrid = 「{結合IsEmptyDataGrid}」>


在我的ViewModel我有物業IsEmptyDataGrid:

public bool IsEmptyDataGrid 
{ 
    get 
    { 
     return _isEmptyDataGrid; 
    } 
    set 
    { 
     _isEmptyDataGrid = value; 
     RaisePropertyChanged("IsEmptyDataGrid"); 
    } 
} 

我的問題是,即使在我的視圖模型的RaisePropertyChanged("IsEmptyDataGrid")被擊中,它不IsEmptyDataGrid屬性裏面去自定義數據網格與源數據一起顯示空消息。

我在做什麼錯?

感謝你在前進, Kruvi

回答

1

DependencyProperty的制定者並不總是叫這個場景。使用回撥功能:

public static readonly DependencyProperty IsEmptyDataGridProperty = DependencyProperty.Register("IsEmptyDataGrid", typeof(bool), typeof(CustomDataGrid), new UIPropertyMetaData(false,Callback)); 

private static void Callback(DependecyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    CustomDatagrid cdt = d as CustomDatagrid; 
    if(cdt._txtEmptyDataGrid != null) 
     { 
      cdt._txtEmptyDataGrid.Visibility = cdt.IsEmptyDataGrid ? Visibility.Visible : Visibility.Collapsed; 
     } 
} 
+0

非常感謝Tenshiko,解決了我的問題! – kruvi 2011-02-09 09:27:33