【问题标题】:Getting value from style setter property in xaml从 xaml 中的样式设置器属性获取值
【发布时间】:2013-05-29 11:24:37
【问题描述】:

如何在 xaml 中获取样式设置器属性的值?

例如,我有下一个样式:

<Style TargetType="TextBox">
    <Setter Property="Background" Value="YellowGreen" />
</Style>

如何从 TextBox 默认样式中获取 Background 属性的值?

<Style TargetType="Button">
    <Setter Property="Background" Value="{Binding ???}" />
</Style>

我需要这个,因为我无法访问TextBox 样式..

【问题讨论】:

    标签: wpf


    【解决方案1】:

    如果你不能修改 TextBox 样式,你可以做这个变通方法(测试,有效):

    <TextBox x:Key="DefaultTextBox" />
    <Style TargetType="Button">
      <Setter Property="Background" 
        Value="{Binding Source={StaticResource DefaultTextBox}, Path=Background}" />
    </Style>
    

    您不能在 xaml 中绑定到样式的背景设置器。

    【讨论】:

      【解决方案2】:

      你应该重构你的 XAML:

      <SolidColorBrush x:Key="BackgroundBrush" Color="YellowGreen" />
      <Style TargetType="TextBox">
          <Setter Property="Background" Value="{StaticResource BackgroundBrush}" />
      </Style>
      
      <Style TargetType="Button">
          <Setter Property="Background" Value="{StaticResource BackgroundBrush}" />
      </Style>
      

      绑定会影响性能,不适用于此类操作。

      【讨论】:

      • 我无法更改 TextBox 样式,我无法访问它。所以我不能用你的方法...
      猜你喜欢
      • 1970-01-01
      • 2013-06-26
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      相关资源
      最近更新 更多