什么不对的约束力? [英] What's wrong with this binding?

查看:139
本文介绍了什么不对的约束力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的DataContext分配给一个菜单项,这是ListBox中的一部分。

I'm trying to assign DataContext to a MenuItem, which is part of ListBox.

    <Style x:Key="ContextMenuStyle" TargetType="telerik:RadMenuItem">
        <Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=telerik:RadListBox}, Path=DataContext}" />
    </Style>

     <DataTemplate x:Key="TemplateSelector">
            <ContentPresenter Content="{Binding}" Name="contentPresenter">
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu>
                        <telerik:RadMenuItem Header="Connect" Click="RadMenuItem_Click" Style="{StaticResource ResourceKey=ContextMenuStyle}" />
                        <telerik:RadMenuItem Header="Disconnect" />
                        <telerik:RadMenuItem Header="Delete Database" />
                    </telerik:RadContextMenu>
                </telerik:RadContextMenu.ContextMenu>
            </ContentPresenter>
     </DataTemplate>


    <Grid>
        <telerik:RadListBox x:Name="lsbDevices" ItemsSource="{Binding Path=Devices}" ItemTemplate="{StaticResource TemplateSelector}" 
                            SelectedItem="{Binding SelectedDevice, Mode=TwoWay}" Grid.Row="0" />
    </Grid>

下面是我做的。 RadListBox的DataContext设置为我的视图模型。我想通过ContextMenuStyle分配这个视图模型的每一个RadMenuItem的DataContext的,但它不工作。 RadListBox的DataContext的已正确设置为我的模型视点,但RadMenuItem的的datacontext为空。我缺少什么?

Here's what I do. RadListBox's DataContext is set to my ViewModel. I want to assign this ViewModel to every RadMenuItem's DataContext through ContextMenuStyle, but it's not working. RadListBox's DataContext is properly set to my modelview, but RadMenuItem's datacontext is null. What am I missing?

感谢

推荐答案

ContextMenus 是不一样的一部分的VisualTree 作为UI的休息,所以你的的RelativeSource 绑定没有找到的ListBox

ContextMenus are not part of the same VisualTree as the rest of the UI, so your RelativeSource binding is not finding the ListBox

您可以找到UI对象的文本菜单使用<$ C的 PlacementTarget 属性附加到$ C>文本菜单

You can find the UI object the ContextMenu is attached to by using the PlacementTarget property of the ContextMenu

<Style x:Key="ContextMenuStyle" TargetType="telerik:RadMenuItem">
    <Setter Property="DataContext" Value="{Binding PlacementTarget.DataContext, 
        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadContextMenu}}}" />
</Style>

这篇关于什么不对的约束力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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