MenuItem IsChecked 属性未绑定到 VM 属性

发布于 2025-01-06 00:25:28 字数 1427 浏览 5 评论 0原文

您好,我是一名新的 WPF/MVVM 程序员,在使用 MenuItem 时遇到问题。

我有一个菜单,其 ItemsSourced 绑定到我创建的对象;

        <Menu Height="23" HorizontalAlignment="Left" Name="menuProfile" VerticalAlignment="Top" Width="58" >
        <MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="IsCheckable" Value="True"/>
                    <Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>
    </Menu>

当我运行此命令时,我可以在菜单中看到我的所有项目,但我的属性 IsSelected 没有更新。

    public bool IsSelected
    {
        get { return _isSelected; }
        set 
        { 
            _isSelected = value;
            GetProfileConfigInfo();        
        }
    }

如果我取出样式容器并将 IsCheckableIsChecked 值与 MenuItem 保持一致......

<MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" IsCheckable="True" IsChecked="{Binding IsSelected}" />

我的属性 IsSelected 会更新,但是我看不到菜单中的任何项目,只能看到标题配置文件

以及我做错了什么的想法???

Hello I am a new WPF/MVVM programmer and having trouble with a MenuItem.

I have a menu who's ItemsSourced are binded to an object I created;

        <Menu Height="23" HorizontalAlignment="Left" Name="menuProfile" VerticalAlignment="Top" Width="58" >
        <MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="IsCheckable" Value="True"/>
                    <Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>
    </Menu>

When I run this I can see all my items in the menu but my property IsSelected isn't getting updated.

    public bool IsSelected
    {
        get { return _isSelected; }
        set 
        { 
            _isSelected = value;
            GetProfileConfigInfo();        
        }
    }

If I take the style container out and put the IsCheckable and IsChecked values in line with the MenuItem....

<MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" IsCheckable="True" IsChecked="{Binding IsSelected}" />

my property IsSelected gets updated but I can't see any of the items in my menu just the header Profiles.

And idea on what I'm doing wrong????

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

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

发布评论

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

评论(1

你与清晨阳光 2025-01-13 00:25:28

在 IsSelected 的 setter 中,您需要调用 OnPropertyChanged("IsSelected")。

我认为这可以解决你的问题。

我将您的确切代码(如下)复制到 Blend 中,并使用您确切的属性名称创建了一个示例数据源,并且菜单中的复选框正常工作。

<Menu Height="23" HorizontalAlignment="Left" Name="menuProfile" VerticalAlignment="Top" Width="58" >
    <MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" >
        <MenuItem.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="IsCheckable" Value="True"/>
                <Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
            </Style>
        </MenuItem.ItemContainerStyle>
    </MenuItem>
</Menu>

In your setter for IsSelected, you need to call OnPropertyChanged("IsSelected").

I think this could fix your problem.

I copied your exact code (below) into Blend, and created a sample data source with your exact property names, and the checkboxes in the menu worked properly.

<Menu Height="23" HorizontalAlignment="Left" Name="menuProfile" VerticalAlignment="Top" Width="58" >
    <MenuItem Header="Profiles" ItemsSource="{Binding Path=ProfileList}" DisplayMemberPath="ProfileName" >
        <MenuItem.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="IsCheckable" Value="True"/>
                <Setter Property="IsChecked" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
            </Style>
        </MenuItem.ItemContainerStyle>
    </MenuItem>
</Menu>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文