【问题标题】:Binding to DataGrid's ItemSource in UserControl在 UserControl 中绑定到 DataGrid 的 ItemSource
【发布时间】:2016-02-14 23:06:21
【问题描述】:

在我的应用程序中,我必须在一个屏幕上显示多个具有相同结构的网格控件(我正在使用 DevExpress),因此我决定为这些网格创建 UserControl。

<UserControl x:Class="MyApp.GridUserControl"
         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" 
         xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
<Border BorderBrush="Black" BorderThickness="1">
    <Grid>
        <dxg:GridControl SelectionMode="Row"
                         AutoGenerateColumns="None">

            <dxg:GridControl.Columns>
                <dxg:GridColumn Header="{x:Static res:Resources.Time}" Binding="{Binding LessonTime}"/>
                <dxg:GridColumn Header="{x:Static res:Resources.Lesson}" Binding="{Binding LessonName}"/>
                <dxg:GridColumn Header="{x:Static res:Resources.Classroom}" Binding="{Binding Classroom}"/>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView AllowEditing="False" AutoWidth="True" ShowGroupPanel="False">
                </dxg:TableView>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Border>

我希望能够在我的 Window 的 xaml 中为此 GridControl 设置 ItemSource。我知道我必须使用 DataContext 属性来执行此操作,但我不知道如何正确使用它。那么,解决这个问题的最佳方法是什么?

【问题讨论】:

  • 您的意思是您的UserControl 将用于一个Window 并且您不想在您发布的这个XAML 中设置ItemsSource,而是在Window 的XAML 中设置ItemsSource?跨度>
  • 是的,这就是我想要实现的目标。我的问题是我必须在我的窗口中使用几个用户控件,所有这些都具有不同的集合作为 ItemSource。
  • 当然,我可以在我的窗口中创建所有的 GridControl,而不使用 UserControl,但由于大量可重复的代码,这不是最好的解决方案。

标签: c# .net wpf xaml mvvm


【解决方案1】:

好的,首先您必须向UserControl 添加一个属性,然后将其用作数据网格的ItemsSource。这应该是这样的:

public partial class GridUserControl : UserControl
{
    public object ItemsSource
    {
        get { return (object)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(object), typeof(GridUserControl), new PropertyMetadata(null));

    //constructor etc.
}

另一种选择是将您的控件基于ItemsControl,而不是UserControl - 然后您从基类中获得ItemsControl 属性。不过,其余部分的工作方式会有所不同,因此我现在将重点关注 UserControl。

下一步是让您的UserControl 中的DataGrid 使用您分配给该属性的任何内容。不确定您的命名空间是什么,请根据需要进行编辑。我只在这里列出相关部分:

<UserControl x:Class="MyApp.GridUserControl"
             xmlns:local="clr-namespace:MyApp">
    <dxg:GridControl ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=local:GridUserControl}}" />

</UserControl>

然后,在您的窗口上,您可以像这样使用它(同样,仅相关部分):

<Window x:Class="MyApp.Window1"
        xmlns:local="clr-namespace:MyApp">
    <Grid>
        <local:GridUserControl ItemsSource="{Binding Items1}"></local:GridUserControl>
        <local:GridUserControl ItemsSource="{Binding Items2}"></local:GridUserControl>
    </Grid>
</Window>        

这假定您的 Window 的 DataContext 是一个具有相关 Items1Items2 属性的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2011-06-05
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 2018-12-28
    • 2023-04-08
    相关资源
    最近更新 更多