【问题标题】:Binding the button Content to the userControl Content将按钮内容绑定到 userControl 内容
【发布时间】:2011-05-22 19:26:42
【问题描述】:

我有一些包含简单按钮的用户控件。 我想将按钮内容绑定到 userControl 内容 - 怎么做?

【问题讨论】:

  • 您想将按钮的 Content 属性绑定到包含该按钮的 userControl 的内容,还是我读错了您的问题?
  • :) 另一种方式 .... userControl 的内容到按钮的内容 ...

标签: wpf binding


【解决方案1】:

为用户控件设置一个名称(例如x:Name="self")并在按钮中

<Button Content={Binding ElementName=self}" />

你的意思是这个还是别的?

【讨论】:

    【解决方案2】:

    如果 Button 在 UserControl 内,它是 UserControl 内容的一部分,不能递归地包含它自己。 UserControl 的全部目的是明确定义一组固定的内容。如果你想要可变的内容,那么你应该使用这样的模板化 ContentControl:

      <ContentControl Content="{Binding SomeVariableValue}">
        <ContentControl.Template>
          <ControlTemplate TargetType="{x:Type ContentControl}">
            <Border>
              <!-- Other content from your user control -->
              <Button Content="{TemplateBinding Content}"/>
            </Border>
          </ControlTemplate>
        </ContentControl.Template>
      </ContentControl>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多