WPF SelectedItem property in nested DataGrid in UserControl

拥有回忆 提交于 2019-12-23 12:29:51

问题


I have UserControl, lets call it as CustomDataGrid, that contains DataGrid. Remained content doesn't matter. SelectedItem property of DataGrid must be SelectedItem property of CustomDataGrid. And I wanna be able to use Binding with this property, cause I use MVVM pattern. So I have to declare SelectedItem as DependencyProperty in CustomDataGrid. But I have no ideas haw can I make it properly...

This is how DepedencyProperty-s is declared usually:

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); }
}

So, how can I declare SelectedItem property correctly?


回答1:


You could leverage the binding framework for wiring such properties from underlying objects to outer containers

example assuming CustomDataGrid as UserControl

        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));
        }

I have created a property called SelectedItem in CustomDataGrid and set a two way binding to SelectedItem of the actual dataGrid inside.

so this will wire up these properties and will propagate any changes to and fro.




回答2:


XAML solution!

Use this 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
});

Then make your outer CustomDataGrid UserControl XAML look like this:

<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>

You can then use the CustomDataGrid control the same way as the DataGrid control when binding ItemsSource and SelectedItem to your view model.



来源:https://stackoverflow.com/questions/24523319/wpf-selecteditem-property-in-nested-datagrid-in-usercontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!