2016-07-22 454 views
-1

我有一个样式如下。WPF样式覆盖Style.Resources属性

<TextBox HorizontalContentAlignment="Center" Text="{Binding IpAddress, Mode=TwoWay}" ToolTip="Ip Address of camera"> 
        <TextBox.Style> 
         <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
          <Style.Resources> 
           <VisualBrush x:Key="CueBannerBrush" AlignmentX="Center" AlignmentY="Center" Stretch="None"> 
            <VisualBrush.Visual> 
             <Label Content="Camera Ip Address" Foreground="Gray" Opacity="0.5" FontStyle="Italic" /> 
            </VisualBrush.Visual> 
           </VisualBrush> 
          </Style.Resources> 
          <Style.Triggers> 
           <Trigger Property="Text" Value="{x:Static sys:String.Empty}"> 
            <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> 
           </Trigger> 
           <Trigger Property="Text" Value="{x:Null}"> 
            <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> 
           </Trigger> 
           <Trigger Property="IsKeyboardFocused" Value="True"> 
            <Setter Property="Background" Value="White" /> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </TextBox.Style> 
       </TextBox>  

而且我一直保持它作为一个Skin.xaml文件资源字典中被重新用作答案here解释。

但是现在我想Content="Camera Ip Address"(样式中的第7行)对于我应用样式的每个文本框都是不同的。我看到SO回答thisthis。这些SO答案提出了BasedOn属性,但我不知道如何将其应用于我的案例。我的情况似乎有很多层次。有人可以建议我如何做到这一点。

基本上我想要的是,对于我申请的内容应摄像机的IP地址,而对于另一个文本框我想这个内容是相机登录一个文本框。我如何实现这一目标?

回答

0

您可以将该内部Label的内容设置为TextBox的Tag属性,然后将其显示在Label中。

像这样:

<TextBox Tag="Whatever you want"> 
    <TextBox.Style> 
     <Style TargetType="TextBox"> 
      <Style.Resources> 
       <VisualBrush x:Key="CueBannerBrush"> 
        <VisualBrush.Visual> 
         <Label Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=TextBox},Path=Tag,Mode=OneWay" /> 
        </VisualBrush.Visual> 
       </VisualBrush> 
      </Style.Resources> 
     </Style> 
    </TextBox.Style> 
</TextBox> 
+0

我的风格有触发器,他们不点火。 :( – VivekDev