WPF:菜单项只能绑定一次命令参数

编程入门 行业动态 更新时间:2024-10-26 18:22:21
本文介绍了WPF:菜单项只能绑定一次命令参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Ive注意到使用菜单命令几次,它们不是很动态,请检查一下。我正在从一组颜色创建一个菜单,我使用它来对数据网格中的列进行着色。无论如何,当我第一次打开菜单(它的上下文菜单)时,命令参数绑定发生,它绑定到上下文菜单打开的列。但是下一次我把它拿起来似乎wpf缓存菜单,它不会重新绑定命令参数。所以我可以在上下文菜单出现的初始列中设置颜色。

过去,我已经解决了这种情况,使菜单完全动态和破坏当菜单关闭并在下次打开时强制进行重新收集,我不喜欢这个黑客。任何人有更好的方法?

< MenuItem 标题=颜色 ItemsSource ={ Binding RelativeSource = {RelativeSource AncestorType = {x:Type local:ResultEditorGrid}},Path = ColumnColourCollection} ItemTemplate ={StaticResource colourHeader}> < MenuItem.Icon> < Image Source ={StaticResource ColumnShowIcon16}/> < /MenuItem.Icon> < MenuItem.ItemContainerStyle> < Style TargetType =MenuItem BasedOn ={StaticResource systemMenuItemStyle}> <! - 警告不要更改以下两个设置器的顺序,否则在命令触发后命令参数设置,不会使用eh? - > < Setter Property =CommandParameter> < Setter.Value> < MultiBinding> < MultiBinding.Converter> < local:ColumnAndColourMultiConverter /> < /MultiBinding.Converter> < Binding RelativeSource ={RelativeSource AncestorType = {x:Type DataGridColumnHeader}}Path =Column/> < Binding Path =。/> < / MultiBinding> < /Setter.Value> < / Setter> < Setter Property =Command Value ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type local:ResultEditorGrid}},Path = ColourColumnCommand}/> < / Style> < /MenuItem.ItemContainerStyle> < / MenuItem>

解决方案

问题是ContextMenu是显然是他们自己的视觉树的根我读到某个地方,它将数据文本作为其父代,但只有一次加载,所以如果父母的数据文本改变男士们没有。 (不幸的是我找不到该链接没有)

我以前遇到过这个问题,我所做的是使用 Josh Smith的虚拟分支模式。这是相当技术性的,但文章帮助我很好地了解了这种视觉树废话是怎么回事。

本质上,您创建了绑定到视图的数据文本的桥。网桥被创建为作为静态资源,允许您从上下文菜单绑定到它,即使它在视觉树之外。

将其添加到您的xaml:

< Window.Resources> ; <! - 这是附加到逻辑树的虚拟分支中的根节点。它的通过Binding设置的DataContext应用于 Window的DataContext属性。 - > < FrameworkElement x:Key =DataContextBridge/> < /Window.Resources> < Window.DataContext> <! - 此Binding在虚拟逻辑树分支的根节点上设置DataContext。这个Binding 必须应用于实际分配了数据上下文值的元素的DataContext。 - > < Binding Mode =OneWayToSource Path =DataContext Source ={StaticResource DataContextBridge} /> < /Window.DataContext>

这是我讲的桥梁。它需要数据文本,并且_ 将它推送到桥接器datacontext,这是(如我之前所说的)静态资源。

然后你简单来说,这是上下文菜单的数据文本:

DataContext ={Binding Source = {StaticResource DataContextBridge}, Path = DataContext}

现在抛出所有相对路径等,并在菜单中使用常规绑定物品,你应该没事。数据文本将照常更新。

只需一个注释:

你显然必须拥有一些属性数据文本来辨别使用哪个命令,但我敢肯定你可以弄清楚。这个解决方案只是处理contextmenu不要更新的方式

Ive noticed this a couple of times when using menus with commands, they are not very dynamic, check this out. I am creating a menu from a collection of colours, I use it to colour a column in a datagrid. Anyway when i first bring up the menu (its a context menu) the command parameter binding happens and it binds to the column that the context menu was opened on. However the next time i bring it up it seems wpf caches the menu and it doesnt rebind the command parameter. so i can set the colour only on the initial column that the context menu appeared on.

I have got around this situation in the past by making the menu totally dynamic and destroying the collection when the menu closed and forcing a rebuild the next time it opened, i dont like this hack. anyone got a better way?

<MenuItem Header="Colour" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ResultEditorGrid}}, Path=ColumnColourCollection}" ItemTemplate="{StaticResource colourHeader}" > <MenuItem.Icon> <Image Source="{StaticResource ColumnShowIcon16}" /> </MenuItem.Icon> <MenuItem.ItemContainerStyle> <Style TargetType="MenuItem" BasedOn="{StaticResource systemMenuItemStyle}"> <!--Warning dont change the order of the following two setters otherwise the command parameter gets set after the command fires, not mush use eh?--> <Setter Property="CommandParameter"> <Setter.Value> <MultiBinding> <MultiBinding.Converter> <local:ColumnAndColourMultiConverter/> </MultiBinding.Converter> <Binding RelativeSource="{RelativeSource AncestorType={x:Type DataGridColumnHeader}}" Path="Column"/> <Binding Path="."/> </MultiBinding> </Setter.Value> </Setter> <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ResultEditorGrid}}, Path=ColourColumnCommand}" /> </Style> </MenuItem.ItemContainerStyle> </MenuItem>

解决方案

The problem is that ContextMenu's are apparently the root of their own visual tree I read somewhere that it takes the datacontext its parent, but only once on loading, so if the parents datacontext changes the menuitems does not. (unfortunately I can't find a link for that right not)

I have encountered this problem before, and what I did was use Josh Smith's Virtual Branch Pattern. It's fairly technical but the article helped me understand really well what was going on with this visual tree nonsense.

Essentially you create this bridge that binds to the view's datacontext. The bridge is created as a static resource, allowing you to bind to it from the context menu even if it is outside the visual tree.

Add this to your xaml:

<Window.Resources> <!-- This is the "root node" in the virtual branch attached to the logical tree. It has its DataContext set by the Binding applied to the Window's DataContext property. --> <FrameworkElement x:Key="DataContextBridge" /> </Window.Resources> <Window.DataContext> <!-- This Binding sets the DataContext on the "root node" of the virtual logical tree branch. This Binding must be applied to the DataContext of the element which is actually assigned the data context value. --> <Binding Mode="OneWayToSource" Path="DataContext" Source="{StaticResource DataContextBridge}" /> </Window.DataContext>

This is the bridge I spoke of. It takes the datacontext and _pushes it to to the bridges datacontext, which is (as I said before) a static resource.

Then you simply this to the contextmenu's datacontext:

DataContext="{Binding Source={StaticResource DataContextBridge}, Path=DataContext}"

Now throw away all the relative pathing etc and use regular binding inside the menu items, and you should be fine. The datacontext will update as usual.

Just one note:

You will obviously have to have some property in the datacontext to discern which command to use, but i'm sure you can figure it out. This solution just deals with the way contextmenu's dont update

更多推荐

WPF:菜单项只能绑定一次命令参数

本文发布于:2023-11-03 13:07:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   菜单项   命令   参数   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!