首页 新闻 会员 周边 捐助

WPF 子元素怎么绑定父元素的属性值

0
悬赏园豆:20 [已解决问题] 解决于 2012-06-25 08:50

我要实现这样一个功能

<ContextMenu x:Key="ContextMenuColoum">
            <MenuItem Header="复制"    Tag="{Binding 这里我要绑定父元素的Text属性值}"   Click="MenCopy_Click" >
                <MenuItem.CommandBindings>
                    <CommandBinding CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" Command="Copy">
                    </CommandBinding>
                </MenuItem.CommandBindings>
            </MenuItem>
        </ContextMenu>
        
        <DataTemplate x:Key="dataTemplateName">
            <TextBlock Text="{Binding Name}" ContextMenu="{ StaticResource ContextMenuColoum}">
            </TextBlock>
        </DataTemplate>

我在MenuItem 中Tag属性绑定该怎么写?

BoyLife的主页 BoyLife | 初学一级 | 园豆:18
提问于:2012-06-06 13:25
< >
分享
最佳答案
0

用相对数据源。就是那个reference还是什么的那个,使得子元素对象的数据源对象是父元素。

收获园豆:20
无之无 | 大侠五级 |园豆:5095 | 2012-06-06 13:33

本人菜鸟啊  具体该怎么实现?

BoyLife | 园豆:18 (初学一级) | 2012-06-06 13:44

@BoyLife: http://msdn.microsoft.com/zh-cn/library/system.windows.data.binding.relativesource(VS.90).aspx

        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
无之无 | 园豆:5095 (大侠五级) | 2012-06-06 13:46

@BoyLife: 

数据绑定(八)使用Binding的RelativeSource - iamsupercola的专...

Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type Grid}}, Path=Name}"  
无之无 | 园豆:5095 (大侠五级) | 2012-06-06 13:48

@笨笨蜗牛: 这个是我想要实现的功能

<ContextMenu x:Key="ContextMenuColoum">
            <MenuItem Header="复制"    Tag="{Binding Path=Text,RelativeSource={  RelativeSource  Mode=FindAncestor, AncestorType={x:Type TextBlock}, AncestorLevel=1 }}"   Click="MenCopy_Click" >
                <MenuItem.CommandBindings>
                    <CommandBinding CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" Command="Copy">
                    </CommandBinding>
                </MenuItem.CommandBindings>
            </MenuItem>
        </ContextMenu>
        
        <DataTemplate x:Key="dataTemplateName">
            <TextBlock Text="{Binding Name}" ContextMenu="{ StaticResource ContextMenuColoum}">
            </TextBlock>
        </DataTemplate>

 

这样写在后台获取不到Tag

我的后台

 

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
              MenuItem men = e.OriginalSource as MenuItem;//获取事件发起源
              if (men != null)
              {
                  e.CanExecute = true;
              }
              else
              {
                  e.CanExecute = false;
              }
            
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MenuItem men = e.OriginalSource as MenuItem;//获取事件发起源
            if (men != null && men.Tag!=null)
            {
                Clipboard.SetText(men.Tag.ToString());
            }
        }

        private void MenCopy_Click(object sender, RoutedEventArgs e)
        {
            MenuItem men = e.OriginalSource as MenuItem;//获取事件发起源
            if (men != null && men.Tag != null)
            {
                Clipboard.SetText(men.Tag.ToString());
            }
        
        }

BoyLife | 园豆:18 (初学一级) | 2012-06-06 14:01

@无之无: 好使~

Jack Su Sparrow | 园豆:153 (初学一级) | 2014-10-28 20:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册