Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable MenuItem based on ObservableCollection empty or not

Tags:

c#

wpf

How can I check in Xaml if an ObservableCollection bound to an ItemsSource of a MenuItem is empty or not? Based on that I would like to enable or disable the MenuItem.

Whats desired:

<MenuItem 
    ItemsSource="{Binding loadables}" 
    Tag="{Binding load}">
    <MenuItem.Triggers>
        <Trigger Property="HasItems" Value="False">
            <Setter Property="IsEnabled" Value="False"/>
        </Trigger>
    </MenuItem.Triggers>
</MenuItem>
like image 452
schwarz Avatar asked Jan 09 '23 10:01

schwarz


1 Answers

You can trigger something when loadables.Count is 0

<MenuItem ItemsSource="{Binding loadables}" Tag="{Binding load}">
    <MenuItem.Style>
        <Style TargetType="{x:Type MenuItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding loadables.Count}" Value="0">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </MenuItem.Style>
</MenuItem>
like image 65
dkozl Avatar answered Jan 21 '23 09:01

dkozl