2011-02-07 41 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