【问题标题】:wpf style ,how to reference border's borderbrush to backgroundwpf样式,如何将边框的边框笔刷引用到背景
【发布时间】:2017-04-10 18:22:44
【问题描述】:

有一个边框,我想同时改变它的边框颜色和背景颜色。 所以,我在下面定义了一个样式

<Style x:Key="EoE" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="{StaticResource LightGreen}"/>
<Setter Property="Background">
    <Setter.Value>
        <SolidColorBrush Color="{Binding Path=BorderBrush }"  Opacity="1"/> 
    </Setter.Value>
</Setter>
<Setter Property="Effect">
    <Setter.Value>
        <DropShadowEffect ShadowDepth="0" Color="White"  Opacity="0.5" BlurRadius="10"/>
        <!--<BlurEffect Radius="3" RenderingBias="Quality"/>-->
    </Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="2"></Setter>

我想在运行时改变的是borderbrush,然后我想同时改变背景solidcolorbrush。

【问题讨论】:

    标签: wpf styles


    【解决方案1】:

    RelativeSource Self进行绑定:

    <Setter Property="Background" 
            Value="{Binding Path=BorderBrush, RelativeSource={RelativeSource Self}"/>
    

    【讨论】:

      【解决方案2】:

      如果你真的想用另一个Opacity 为边框的Background 属性定义另一个画笔,你可以试试这个:

      <Style x:Key="EoE" TargetType="{x:Type Border}">
          <Style.Resources>
              <SolidColorBrush x:Key="bgBrush" Color="{Binding Path=BorderBrush.(SolidColorBrush.Color), RelativeSource={RelativeSource AncestorType=Border}}" Opacity="0.7"/>
          </Style.Resources>
          <Setter Property="BorderBrush" Value="{StaticResource LightGreen}"/>
          <Setter Property="Background" Value="{StaticResource bgBrush}" />
          <Setter Property="Effect">
              <Setter.Value>
                  <DropShadowEffect ShadowDepth="0" Color="White"  Opacity="0.5" BlurRadius="10"/>
              </Setter.Value>
          </Setter>
          <Setter Property="BorderThickness" Value="2"></Setter>
      </Style>
      

      如果您只想将Background 设置为与BorderBrush 完全相同的画笔,您可以使用@ASh 提供的解决方案:

      <Style x:Key="EoE" TargetType="{x:Type Border}">
          <Setter Property="BorderBrush" Value="{StaticResource LightGreen}"/>
          <Setter Property="Background"  Value="{Binding Path=BorderBrush, RelativeSource={RelativeSource Self}}"/>
          <Setter Property="Effect">
              <Setter.Value>
                  <DropShadowEffect ShadowDepth="0" Color="White"  Opacity="0.5" BlurRadius="10"/>
              </Setter.Value>
          </Setter>
          <Setter Property="BorderThickness" Value="2"></Setter>
      </Style>
      

      【讨论】:

        猜你喜欢
        • 2011-10-24
        • 2018-05-12
        • 2011-06-16
        • 2011-12-07
        • 1970-01-01
        • 2012-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多