我正在尝试设置一个MenuItem,它将具有可以选择的页码子菜单.我想将ItemsSource绑定到页码列表(实际上是带有创建列表的转换器的PageCount),然后将子菜单中每个MenuItem的IsChecked属性绑定到PageIndex.我的问题是第二个绑定,因为它也需要转换器,但转换器需要知道MenuItem表示的页码,但我无法弄清楚如何将该信息传递给转换器.这是我到目前为止所尝试的内容.
<MenuItem Header="_Goto Page"
ItemsSource="{Binding
Path=CurrentImage.PageCount,
Converter={StaticResource countToList}}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="IsCheckable"
Value="True"/>
<Setter Property="IsChecked"
Value="{Binding
ElementName=MainWindow,
Path=CurrentImage.PageIndex,
Mode=TwoWay,
Converter={StaticResource pageNumChecked},
ConverterParameter={Binding
RelativeSource={RelativeSource Self},
Path=Content}}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
当然问题是ConverterParameter不能绑定,因为它不是DependencyProperty.所以我的问题是我如何传递我需要的信息,或者有另一种方法来做到这一点.
注意:我已经尝试将MenuItem放在ListBox中,就绑定而言,它非常有效,但导致子菜单以非标准方式运行.也就是说,当您打开子菜单时,整个ListBox被视为一个MenuItem.
编辑
所以这就是我到目前为止所做的工作.我尝试绑定到一个隐藏的ListBox但是当我将MenuItem.ItemsSource绑定到’ListBox.Items’时,我获得了int列表而不是ListBoxItems列表,我需要获取IsSelected属性.所以我最终使用了Quartermeister的建议,即使用MultiBinding将IsChecked属性绑定到OneWay模式下的PageIndex.为了处理另一个方向,我在Click事件上使用了一个事件处理程序.值得注意的是,起初我将IsCheckable设置为true并且正在使用Checked事件,但结果是一些奇怪的行为.
<MenuItem x:Name="GotoPageMenuItem" Header="_Goto Page"
ItemsSource="{Binding Path=CurrentImage.PageCount,
Converter={StaticResource countToList}}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="IsCheckable"
Value="False"/>
<EventSetter Event="Click"
Handler="GotoPageMenuItem_Click"/>
<Setter Property="IsChecked">
<Setter.Value>
<MultiBinding Converter="{StaticResource pageNumChecked}"
Mode="OneWay">
<Binding RelativeSource="{RelativeSource FindAncestor,
AncestorType={x:Type Window}}"
Path="CurrentImage.PageIndex"
Mode="OneWay"/>
<Binding RelativeSource="{RelativeSource Self}"
Path="Header"
Mode="OneWay"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
这是GotoPageMenuItem_Click代码
private void GotoPageMenuItem_Click(object sender, RoutedEventArgs e)
{
var item = sender as MenuItem;
if (item != null)
{
//If the item is already checked then we don't need to do anything
if (!item.IsChecked)
{
var pageNum = (int)item.Header;
CurrentImage.PageIndex = (pageNum - 1);
}
}
}
解决方法:
你能用MultiBinding做你想做的事吗?
<Setter Property="IsChecked">
<Setter.Value>
<MultiBinding Converter="{StaticResource pageNumChecked}">
<Binding ElementName="MainWindow" Path="CurrentImage.PageIndex" Mode="TwoWay"/>
<Binding RelativeSource="{RelativeSource Self}" Path="Content"/>
</MultiBinding>
</Setter.Value>
</Setter>
让pageNumChecked转换器实现IMultiValueConverter而不是IValueConverter,转换将获得一个数组,其中包含每个子绑定的结果.在这种情况下,它将是一个双元素数组,其中第一个元素是您当前的输入,PageIndex,第二个索引是您当前的ConverterParameter,Content.如果你想要双向绑定,ConvertBack将需要返回一个双元素数组,你将为第二个参数返回Binding.DoNothing,这样它就不会尝试更新内容.
标签:c,wpf,binding,menuitem 来源: https://codeday.me/bug/20190518/1129989.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。