WPF context menu bound to List <> dependency property

I am trying to make the contents of a list whose dependency property appears in the WPF context menu.

I have a class with the following dependency property, list Foo (data storage class):

    public List<Foo> FooList
    {
        get { return (List<Foo>)GetValue(FooListProperty); }
        set { SetValue(FooListProperty, value); }
    }
    public static DependencyProperty FooListProperty =
        DependencyProperty.Register("FooList", typeof(List<Foo>),
            typeof(FooButton));

In XAML, I set the following static resource, I assume it is necessary, since the context menu is not part of the visual tree:

<UserControl.Resources>
    <ResourceDictionary>            
        <CollectionViewSource 
            x:Key="FooListSource"
            Source="{Binding FooList}"/>

        <!-- ... -->

    </ResourceDictionary>
</UserControl.Resources>

ResourceDictionary CompositeCollection, , . UserControl CanStop true, . , MenuItems . , , , .

<CompositeCollection x:Key="FooListItems">
    <CollectionContainer 
        Collection="{Binding Source={StaticResource FooListSource}}"/>
    <Separator 
        Visibility="{Binding CanStop,
            Converter={StaticResource VisibleIfTrue}}" />
    <MenuItem 
        Command="{x:Static Buttons:FooButton.Stop}"
        Header="Stop"
        Visibility="{Binding CanStop,
            Converter={StaticResource VisibleIfTrue}}"/>
</CompositeCollection>

, , , ResourceDictionary:

<ContextMenu 
    x:Key="FooButtonMenu"
    ItemsSource="{Binding Source={StaticResource FooListItems}}" 
    ItemTemplate="{StaticResource FooListTemplate}"
    <ContextMenu.CommandBindings>
        <CommandBinding  
                Command="{x:Static Buttons:FooButton.Stop}"
                Executed="Stop_Executed" />
    </ContextMenu.CommandBindings>
</ContextMenu>

, , , . menuitem. - . , , -, , .

.:)

+3
2

, , , List<Foo> ObservableCollection<Foo>. List<Foo> , WPF , , - FooList - , .

CLR. List<Foo> ObservableCollection<Foo>.

, CompositeCollection , , CompositeCollection DependencyObject, a DataContext.

+1

, FooList. , . , ( ). . ?

, , FooList CLR- ObservableCollection<Foo> ( , INotifyCollectionChanged). , - , , .

0

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


All Articles