我试图用Paging创建一个Datagrid,但我还没有那么远。 现在我有了这个。
<UserControl x:Class="SampleControls.UserControls.PageFilterGrid"
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:SampleControls.UserControls"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="500"
Name="Ultra">
<Grid>
<DataGrid Name="MyGrid" AutoGenerateColumns="{Binding ElementName=Ultra, Path=AutoGenerateColumns}" ItemsSource="{Binding ElementName=Ultra, Path=MyItemsSource}" />
</Grid>
该控件的Codebehind如下所示:
public partial class PageFilterGrid : UserControl
{
public PageFilterGrid()
{
InitializeComponent();
}
public static readonly DependencyProperty MyItemsSourceProperty = DependencyProperty.Register("MyItemsSource",
typeof(IEnumerable), typeof(PageFilterGrid));
public static readonly DependencyProperty GenerateColumns = DependencyProperty.Register("AutoGenerateColumns",
typeof(bool), typeof(PageFilterGrid), new PropertyMetadata(false));
public static readonly DependencyProperty MyUltraGrid = DependencyProperty.Register("UltraGrid",
typeof(DataGrid), typeof(PageFilterGrid), new PropertyMetadata(new DataGrid()));
public IEnumerable MyItemsSource
{
get
{
return (IEnumerable)GetValue(PageFilterGrid.MyItemsSourceProperty);
}
set
{
SetValue(PageFilterGrid.MyItemsSourceProperty, value);
}
}
public bool AutoGenerateColumns
{
get
{
return (bool)GetValue(PageFilterGrid.GenerateColumns);
}
set
{
SetValue(PageFilterGrid.GenerateColumns, value);
}
}
public DataGrid UltraGrid {
get
{
return (DataGrid)GetValue(PageFilterGrid.MyUltraGrid);
}
set
{
SetValue(PageFilterGrid.MyUltraGrid, value);
}
}
}
我的问题是,如果我不想自动生成列,我如何指定列而不在代码后面添加它们?我想像在XAML中一样将它们添加到网格中。下面是这样的,但这不起作用。
我尝试过将这些列重新设置并设置该属性,但这似乎对XAML无效。
<controls:PageFilterGrid x:Name="myultra" AutoGenerateColumns="False" MyItemsSource="{Binding MyCollection}">
<controls:PageFilterGrid.UltraGrid>
<controls:PageFilterGrid.UltraGrid.Columns>
<DataGridTextColumn Header="amount" Binding="{Binding}" />
</controls:PageFilterGrid.UltraGrid.Columns>
</controls:PageFilterGrid.UltraGrid>
</controls:PageFilterGrid>