UserControl中嵌套DataGrid中的WPF SelectedItem属性

时间:2014-07-02 05:12:50

标签: wpf mvvm datagrid user-controls selecteditem

我有UserControl,我们称之为CustomDataGrid,其中包含DataGrid。剩下的内容无关紧要。 SelectedItem DataGrid的{​​{1}}属性必须是SelectedItem CustomDataGrid的{​​{1}}属性。我希望能够将Binding与此属性一起使用,因为我使用MVVM模式。因此,我必须在SelectedItem中将DependencyProperty声明为CustomDataGrid。但是我没有想法,我可以正确地做到这一点......

这通常是DepedencyProperty - s的声明:

public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
        "SelectedItem", typeof(Object), typeof(CustomDataGrid),
        new FrameworkPropertyMetadata(default(Object), SelectedItemPropertyCallback)
        {
            BindsTwoWayByDefault = true, 
            DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        });

// Optionally
private static void SelectedItemPropertyCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    // dataGrid - `DataGrid` nested in `UserControl` 
    ((CustomDataGrid)obj).dataGrid.SelectedItem = e.NewValue;
}

// Obviously, it has no any link with nested `dataGrid`. This is the problem.  
public Object SelectedItem
{

    get { return GetValue(SelectedItemProperty); }
    set { SetValue(SelectedItemProperty, value); }
}

那么,我如何正确声明SelectedItem属性?

2 个答案:

答案 0 :(得分:2)

您可以利用绑定框架将这些属性从底层对象连接到外部容器

示例CustomDataGridUserControl

        class CustomDataGrid : UserControl
        {
            public CustomDataGrid()
            {
                Binding b = new Binding("SelectedItem");
                b.Source = this;
                b.Mode = BindingMode.TwoWay;
                dataGrid.SetBinding(DataGrid.SelectedItemProperty, b);
            }

            public object SelectedItem
            {
                get { return (object)GetValue(SelectedItemProperty); }
                set { SetValue(SelectedItemProperty, value); }
            }

            // Using a DependencyProperty as the backing store for SelectedItem.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty SelectedItemProperty =
                DependencyProperty.Register("SelectedItem", typeof(object), typeof(CustomDataGrid), new PropertyMetadata(null));
        }

我在SelectedItem中创建了一个名为CustomDataGrid的属性,并设置了与内部实际SelectedItem的{​​{1}}的双向绑定。

因此,这将连接这些属性,并将传播任何更改。

答案 1 :(得分:2)

XAML解决方案!

使用此DependencyProperty:

public object SelectedItem
{
    get { return GetValue(SelectedItemProperty); }
    set { SetValue(SelectedItemProperty, value); }
}  

public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(CustomDataGrid ), new FrameworkPropertyMetadata(null)
{
    BindsTwoWayByDefault = true,
    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});

然后让你的外部CustomDataGrid UserControl XAML看起来像这样:

<UserControl x:Class="CustomDataGrid">
    <DataGrid ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CustomDataGrid}}}"
              SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CustomDataGrid}}}">

    </DataGrid>
</UserControl>

然后,在将ItemsSource和SelectedItem绑定到视图模型时,可以使用与DataGrid控件相同的方式使用CustomDataGrid控件。