MenuItem IsChecked 属性未绑定到 VM 属性
您好,我是一名新的 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();
}
}
如果我取出样式容器并将 IsCheckable 和 IsChecked 值与 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 IsSelected 的 setter 中,您需要调用 OnPropertyChanged("IsSelected")。
我认为这可以解决你的问题。
我将您的确切代码(如下)复制到 Blend 中,并使用您确切的属性名称创建了一个示例数据源,并且菜单中的复选框正常工作。
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.