【发布时间】:2015-04-14 20:43:02
【问题描述】:
DI 正在尝试将 DataGrid.Columns 属性公开给 Ancestor UserControl,就像我使用 answer here 对项目源所做的那样。似乎没有任何属性可供我按以下方式绑定 DataColumn:
<PagedDataGrid>
<PagedDataGrid.Columns>
<DataGridTextColumn Header="Custom Column"/>
<PagedDataGrid.Columns>
</PagedDataGrid>
这是我目前所拥有的。我需要在后面的代码中以某种方式执行此操作吗?
XAML:
<UserControl x:Class="WpfPagedGrid.PagedDataGrid"
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"
xmlns:local="clr-namespace:WpfPagedGrid"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<WrapPanel><TextBlock>Paging Controls...</TextBlock></WrapPanel>
<DataGrid Name="dataGrid" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType=local:PagedDataGrid, AncestorLevel=1}, Path=ItemsSource}">
</DataGrid>
</Grid>
</UserControl>
C#:
namespace WpfPagedGrid {
public partial class PagedDataGrid : UserControl {
public PagedDataGrid() { InitializeComponent(); }
public IEnumerable ItemsSource {
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty = DataGrid.ItemsSourceProperty.AddOwner(typeof(PagedDataGrid));
//Does not work as there is no DataGrid.Columns property...
//public ObservableCollection<DataGridColumn> Columns {
//get { return (ObservableCollection<DataGridColumn>)GetValue(ColumnsProperty); }
//set { SetValue(ItemsSourceProperty, value); }
//}
//public static readonly DependencyProperty ColumnsProperty = DataGrid.CloumnsProperty.AddOwner(typeof(PagedDataGrid));
}
}
编辑: 为了澄清,我试图在用户控件上指定列并将 DataGrid 绑定到这些列。
【问题讨论】:
-
您是否尝试在用户控件上指定列并将 DataGrid 绑定到这些列?或者你想让用户控件知道 DataGrid 有哪些列?
-
我正在尝试在用户控件上指定列并将 DataGrid 绑定到这些列。
标签: c# wpf xaml datagrid user-controls