从上下文菜单中绑定的ElementName找不到目标 [英] Binding ElementName from context menu can't find target

查看:137
本文介绍了从上下文菜单中绑定的ElementName找不到目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从下拉菜单按钮内部上下文菜单绑定到一个元素(从的 http://shemesh.wordpress.com/2011/10/27/wpf-menubutton/ )。即使在上下文菜单之外的结合似乎工作,上下文菜单里面的结合并不

I am trying to bind to an element from a context menu inside a drop-down menu button (from http://shemesh.wordpress.com/2011/10/27/wpf-menubutton/). Even though outside the context menu the binding seems to work, the binding inside the context menu does not.

这是XAML(非常简单):

This is the XAML (very simplified):

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" CanContentScroll="False">
            <ListBox x:Name="lbScenarios"  HorizontalContentAlignment="Stretch">
                <ItemsControl.Template>
                    <ControlTemplate TargetType="ItemsControl">
                        <ItemsPresenter Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualWidth}"/>
                    </ControlTemplate>
                </ItemsControl.Template>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border>
                            <Expander>
                                <Expander.Header>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Margin="5,0,0,0" Grid.Column="0" VerticalAlignment="Center">Results</TextBlock>
                                        <local:MenuButton Grid.Column="3" Content="Menu" Margin="5,0,0,0" VerticalAlignment="Center">
                                            <local:MenuButton.Menu>
                                                <ContextMenu>
                                                    <MenuItem Header="Save pie chart as image"
                                                                              Command="{Binding SaveChartImageCommand}"
                                                                              CommandParameter="{Binding ElementName=pieChart}" />
                                                    <MenuItem Header="Save bar chart as image"
                                                                              Command="{Binding SaveChartImageCommand}"
                                                                              CommandParameter="{Binding ElementName=barChart}" />
                                                </ContextMenu>
                                            </local:MenuButton.Menu>
                                        </local:MenuButton>
                                    </Grid>
                                </Expander.Header>
                                <Expander.Content>
                                    <StackPanel>
                                        <Image x:Name="pieChart" />
                                        <Image x:Name="barChart" />
                                    </StackPanel>
                                </Expander.Content>
                            </Expander>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ListBox>
        </ScrollViewer>
    </Grid>
</Window>



绑定不起作用是{结合的ElementName =饼图},这是有趣的,因为该命令被发现。我似乎无法得到的RelativeSource工作,但有人可以帮助我得到正确的结合?

The binding that does not work is the {Binding ElementName=pieChart}, which is funny because the command is being found. I couldn't seem to get a RelativeSource to work, but can someone help me with getting the binding correct?

推荐答案

由于< STRONG>的文本菜单并不在同一视觉树,它的目标位置,这样的ElementName绑定将无法正常工作的,因为它需要两个控件是在相同的可视树。

Since ContextMenu doesn't lie in same Visual tree as that of its placement target so ElementName binding won't work because it requires both controls to be in same Visual tree.

尝试使用 X:参考 不具备这个约束是在相同的视觉。树

Try using x:Reference which doesn't have this constraint of to be in same visual tree.

CommandParameter="{Binding Source={x:Reference pieChart}}"

使用它像这样

CommandParameter="{x:Reference pieChart}"

注意的 - X:参考将在WPF 4.0或更高版本中找到

Note - x:Reference will be found in WPF 4.0 or later.

这篇关于从上下文菜单中绑定的ElementName找不到目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆