【发布时间】:2015-06-30 08:51:18
【问题描述】:
我正在编写一个包含三个 DataGrid(DG_Top、DG_Center、DG_Bottom)的新控件。
当试图为这个控件设置数据时,什么也没有发生。 ContextProperties(见下文)已填充,但未显示数据。
在 UserControl (XGrid) 中,我创建了一些属性来管理 DataBindings:
public static readonly DependencyProperty ItemsSource_TopProperty = DependencyProperty.Register("ItemsSource_Top", typeof(IEnumerable), typeof(XGrid));
public static readonly DependencyProperty ItemsSource_CenterProperty = DependencyProperty.Register("ItemsSource_Center", typeof(IEnumerable), typeof(XGrid));
public static readonly DependencyProperty ItemsSource_BottomProperty = DependencyProperty.Register("ItemsSource_Bottom", typeof(IEnumerable), typeof(XGrid));
public static readonly DependencyProperty DataContext_TopProperty = DependencyProperty.Register("DataContext_Top", typeof(object), typeof(XGrid));
public static readonly DependencyProperty DataContext_CenterProperty = DependencyProperty.Register("DataContext_Center", typeof(object), typeof(XGrid));
public static readonly DependencyProperty DataContext_BottomProperty = DependencyProperty.Register("DataContext_Bottom", typeof(object), typeof(XGrid));
public IEnumerable ItemsSource_Top
{
get { return (IEnumerable)GetValue(ItemsSource_TopProperty); }
set
{
SetValue(ItemsSource_TopProperty, value);
OnPropertyChanged();
}
}
public IEnumerable ItemsSource_Center...
public IEnumerable ItemsSource_Bottom...
public object DataContext_Top
{
get { return (object)GetValue(DataContext_TopProperty); }
set
{
SetValue(DataContext_TopProperty, value);
OnPropertyChanged();
}
}
public object DataContext_Center...
public object DataContext_Bottom...
XGrid.xaml 中的绑定如下:
<UserControl x:Class="WPF.XGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Name="control">
<StackPanel>
<DataGrid ItemsSource="{Binding ItemsSource_Top, ElementName=control}"
DataContext="{Binding DataContext_Top, ElementName=control}"
...
我使用 XGrid 控件如下(在 xaml 中):
<iw:XGrid x:Name="XG_Test" ItemsSource_Center="{Binding}" />
在.cs文件中:
DataTable dt = new DataTable();
dt.Columns.Add("TEST");
dt.Rows.Add(new string[] { "" });
XG_Test.DataContext_Center = dt.DefaultView;
谁能告诉我为什么绑定不起作用? 谢谢!
-------编辑 1-------
Visual Studio 输出没有说明任何错误
【问题讨论】:
-
检查 Visual Studio 的输出选项卡,或者使用像 Snoop 这样的工具来检查绑定的属性。您应该能够以这种方式看到实际的 Binding 错误,并知道真正失败的原因。
-
但除此之外......您是否设置了三个
DataContext_x属性?您确定要设置的 ItemsSource 在该 DataContext 中吗?因为在您的示例中,您设置的是ItemsSource_Center而不是DataContext_Center,这将默认为null,因此您的{Binding}将不绑定任何内容。 -
@almulo 请参阅编辑 1 我在 .cs 文件中设置 DataContext(编辑 1 正上方)=>
XG_Test.DataContext_Center = dt.DefaultView; -
好的,我想我知道问题出在哪里了。您的 DataGrid 的 ItemsSource 绑定没有考虑 DataGrid DataContext,而是在
XGrid控件的 DataContext 中搜索其值。 -
@almulo 是的,你是对的,刚刚测试了它设置
XG_Test.DataContext = dt.DefaultView;(显示 DG_Center 中的数据)但是我怎样才能让 DataGrid 使用它自己的上下文?
标签: wpf xaml data-binding datagrid dependency-properties