为什么我的绑定不起作用? [英] Why my Binding dosen't work?

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

问题描述

我的朋友

我有一个像下面这样的XAML文件:

Hi my friends
i have a XAML file like follwoing:

<Grid>
        <Label x:Name="AttachmentNameLabel" Content="Attachment"
               Height="35" Width="80" Background="AliceBlue"
               BorderBrush="LightGray" BorderThickness="1.5" VerticalContentAlignment="Center">
            <Label.ContextMenu>
                <ContextMenu>
                    <MenuItem x:Name="PreviewMenuItem" Header="{Binding ElementName=AttachmentNameLabel,Path=Tag}"/>
                </ContextMenu>
            </Label.ContextMenu>
        </Label>
    </Grid>



但是MenuItem.Header绑定不起作用。为什么?

请代表解决方案。

谢谢


but MenuItem.Header binding doesn't work. why?
please represent solution.
thanks

推荐答案

你好



ContextMenu中的绑定不像你想象的那样工作。



ContextMenu是每个wpf控件都拥有的东西因此ContextMenu拥有一个名为PlacementTarget的属性正如名称所示,标识将放置ContextMenu的目标控件。



现在回答你的问题。如果你想在ContextMenu中使用Binding,你必须这样做:



Hello

Binding in ContextMenu doesn't work the way you think.

ContextMenu is something every wpf control possess therefore ContextMenu holds a property called PlacementTarget which as the name says identifies the target control where ContextMenu will be placed.

Now to your question. If you wish to use Binding in ContextMenu you will have to do it this way:

<stackpanel tag="this will be the header">
   <contextmenu>
           <menuitem>
               Header="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"
           </menuitem>
    </contextmenu>
</stackpanel>





看看我想说什么?在我的示例中,PlacementTarget是StackPanel。



See what I am trying to say? In my example PlacementTarget is the StackPanel.


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

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