将 MenuItem 属性 (IsEnabled) 绑定到同一控件中的组合框属性 (SelectedIndex)

发布于 2024-09-11 12:13:30 字数 1055 浏览 3 评论 0原文

我花了一天的大部分时间来研究这个问题;我很好奇是否可以在 XAML 中完全执行简单绑定,而不需要在后面的代码中实现 INotifyPropertyChanged。

讽刺的是,就我花在研究这个问题上的时间而言,我本可以在后台代码中完成 5 次以上。

我遇到过一些建议使用 DataTriggers 的文章(对于 MenuItems,DataTrigger 必须位于 Style Trigger 内)。我已经尝试过这个,但它不能正常工作。

我怀疑 DataTrigger 由于 MenuItem 范围问题而找不到组合框,我在另一个线程中读取了该问题。

有人有什么建议吗?

代码:(没有构建或运行时错误,但属性未更新)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-09-18 12:13:30

我将默认设置器移至 Style.Triggers 之前(由于编译错误),将所有内容放入 Menu (以简化示例)并使其在索引 上触发code>0 (更好地演示结果)。以下作品:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文