【问题标题】:What's the "value" property in Binding tag?Binding 标记中的“值”属性是什么?
【发布时间】:2018-08-10 18:42:50
【问题描述】:

我有一个新手问题,但我找不到答案。我什至不知道如何问谷歌这个问题。

假设你有:

Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}, AncestorLevel=2}}"

如何做到这一点?:

<Button.Tag>
    <Binding ??? RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}, AncestorLevel=2}" />
</Button.Tag>

我已经尝试过“内容”“价值”“来源”..感谢您的帮助!

【问题讨论】:

  • Tag 本质上是Button 控件的属性。那么为什么不采用第一种方式呢?
  • 如果必须的话,您可以在 Tag 中添加一个 ContentControl 并执行此操作,但我真的不明白您为什么要这样做:&lt;Button.Tag&gt; &lt;ContentControl&gt; &lt;TextBlock Text="{Binding ...}"/&gt; &lt;/ContentControl&gt; &lt;/Button.Tag&gt;
  • 为什么不直接做 呢?没有真正的理由尝试第二种方式。
  • @Sach "tag" 也是 Binding Button 或任何.. 标签的 HTML 词。在 Html 中有HTML 标签、BODY 标签、&lt;P&gt; 标签等
  • @robertwojnar HTML 或 XML 或 XAML 标记只是一个语法元素,即用尖括号括起来的标识符。它与数据绑定无关,也与FrameworkElement.Tag Property完全无关。

标签: c# wpf xaml mvvm binding


【解决方案1】:
<Button ...>
    <Button.Tag>
        <Binding .../>
    </Button.Tag>
</Button>

等价于

<Button Tag="{Binding ...}" />

但是,您缺少 Binding 的源属性路径。添加Path="DataContext"

<Button.Tag>
    <Binding Path="DataContext" .../>
</Button.Tag>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-25
    • 2014-03-19
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多