WPF SelectedItem property in a nested DataGrid in UserControl

I have UserControl, let's call it CustomDataGrid, containing DataGrid. The remaining content does not matter. SelectedItema property DataGridmust be a SelectedItemproperty CustomDataGrid. And I want to use Bindingwith this property because I use a template MVVM. So I have to declare SelectedItemas DependencyPropertyin CustomDataGrid. But I have no ideas, I can do it right ...

This is how DepedencyProperty-s is usually declared :

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 a property correctly SelectedItem?

+4
source share
2 answers

.

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

SelectedItem CustomDataGrid SelectedItem dataGrid .

.

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

XAML CustomDataGrid UserControl :

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

CustomDataGrid , DataGrid ItemsSource SelectedItem .

+2

Source: https://habr.com/ru/post/1546779/


All Articles