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
非常感谢Tenshiko,解决了我的问题! – kruvi 2011-02-09 09:27:33