开发者

Binding MenuItem property (IsEnabled) to combobox property (SelectedIndex) in same control

开发者 https://www.devze.com 2023-01-09 02:30 出处:网络
I\'ve spent a good portion of my day researching this; I\'m curious if it\'s possible to do simple binding fully in XAML, without the need to implement INotifyPropertyChanged in the code behind.

I've spent a good portion of my day researching this; I'm curious if it's possible to do simple binding fully in XAML, without the need to implement INotifyPropertyChanged in the code behind.

Ironically, in the amount of time I've spent researching this I could have just done it in the code behind 5 times over.

I've come across a few articles that suggest using DataTriggers (for MenuItems, the DataTrigger must be inside the a Style Trigger). I've tried this, but it doesn't work without error.

I suspect the DataTrigger couldn't find the combobo开发者_Go百科x because of MenuItem scope issues, which I read in a different thread.

Anyone have any suggestions?

Code: (no build or runtime errors, but property not updated)

<ContextMenu>
<MenuItem Header="Do Something Neat" x:Name="MyMenuItem" Click="MyMenuItem_Click">
    <MenuItem.Style>
        <Style TargetType="{x:Type MenuItem}">
            <Style.Triggers>
                <Setter Property="IsEnabled" Value="True" />
                <DataTrigger Binding="{Binding ElementName=MyComboBox, Path=SelectedIndex}" Value="-1">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>                                        
            </Style.Triggers>
        </Style>
    </MenuItem.Style>
</MenuItem>
</ContextMenu>


I moved the default setter to before Style.Triggers (because of a compile error), put everything into a Menu (to simplify the example) and made it trigger on index 0 (to better demonstrate the result). The following works:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel LastChildFill="False">
        <Menu Height="23" DockPanel.Dock="Top" >
            <MenuItem Header="Do Something Neat">
                <MenuItem.Style>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="IsEnabled" Value="True" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=MyComboBox, Path=SelectedIndex}" Value="0">
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.Style>
            </MenuItem>
        </Menu>
        <ComboBox Name="MyComboBox" Height="23" Width="120" DockPanel.Dock="Top" >
            <ComboBoxItem >Index0</ComboBoxItem>
            <ComboBoxItem >Index1</ComboBoxItem>
        </ComboBox>
    </DockPanel>
</Window>
0

精彩评论

暂无评论...
验证码 换一张
取 消