2017-02-16 41 views
1

在我的项目中,我有多个不同的必需控件。为了让用户知道一个字段是必需的,我使用DataTrigger在该字段上设置了一个红色轮廓。如何在DataTrigger中使用预定义的样式

这里是一个文本框是必填字段:

<TextBox Text="{Binding RationaleForNoInvestigation, UpdateSourceTrigger=PropertyChanged}"> 
       <TextBox.Style> 
       <Style TargetType="TextBox"> 
        <Style.Triggers> 
        <DataTrigger Binding="{Binding RationaleForNoInvestigation, 
Converter={StaticResource IsNullOrEmptyStringConverter}}" 
           Value="True"> 
         <Setter Property="BorderBrush" 
           Value="Red" /> 
         <Setter Property="BorderThickness" 
           Value="1" /> 
        </DataTrigger> 
        </Style.Triggers> 
       </Style> 
       </TextBox.Style> 
      </TextBox> 

这里是一个组合框是必填字段:

<telerik:RadComboBox SelectedItem="{Binding Path=SelectedRoomType, Mode=TwoWay}"> 
    <telerik:RadComboBox.Style> 
     <Style TargetType="telerik:RadComboBox"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=SelectedRoomType, 
Converter={StaticResource IsNullConverter}}" 
         Value="True"> 
      <Setter Property="BorderBrush" 
        Value="Red" /> 
      <Setter Property="BorderThickness" 
        Value="2" /> 
      </DataTrigger> 
     </Style.Triggers> 
     </Style> 
    </telerik:RadComboBox.Style> 
    </telerik:RadComboBox> 

不过,我可能有一天决定改变视觉而是指示一个必填字段,而不是蓝色背景。 我不想在我的项目中使用样式并手动更改它的所有地方。相反,我想设置一个全局风格,我可以在特定控件中引用它,然后可以在一个地方更改全局风格。

如何从上面的代码中提取这些代码并放到全局位置?我怎么会在上面的TextBox和ComboBox中引用它?

<Setter Property="BorderBrush" 
          Value="Red" /> 
<Setter Property="BorderThickness" 
          Value="1" /> 

回答

3

您可以创建一个单独的包含所有样式的XAML文件!

由于您需要定位多个控件,因此我建议为它们中的每一个创建不同的样式,因为您可能需要更改不同的属性。它应该是这样的:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <converters:IsNullConverter x:Key="IsNullOrEmptyStringConverter" /> 

    <!-- I also highly recommend creating the color brush separately, since this enables you to change the color without changing each one of the styles --> 
    <SolidColorBrush Color="Red" x:Key="ErrorBrush" /> 

    <Style x:Key="ValidateTextBoxStyle" TargetType="TextBox"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Text, Source={RelativeSource AncestorType=TextBox}, Converter={StaticResource IsNullOrEmptyStringConverter}}" Value="True"> 
       <Setter Property="BorderBrush" Value="{StaticResouce ErrorBrush}" /> 
       <Setter Property="BorderThickness" Value="1" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 

</ResourceDictionary> 

您可以参考在视图的XAML文件,就像这样:

<MainView.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</MainView.Resources> 

这样,您就可以在任何地方引用您的风格在你的应用程序中,如果您引用样式XAML!

<TextBox 
    Text="{Binding RationaleForNoInvestigation, UpdateSourceTrigger=PropertyChanged}" 
    Style="{StaticResource ValidateTextBoxStyle} 
    /> 
+1

你为什么建议把样式放在单独的文件中?我们通常将它们放在App.xaml文件中。我知道一个单独的文件允许在不同的应用程序中重用,但是这个应用程序有没有优势? –

+1

@AvrohomYisroel我想这取决于你的应用程序的大小。例如,在工作中,我们至少有100个不同的XAML文件。想象一下只有一个文件中的所有样式!我猜Rose的情况可能不是一个巨大的应用程序,但没有人知道,对吧? –

+1

这对我有帮助,但我有几个问题: 1.分开保存笔刷颜色非常聪明,如果我决定改变笔刷颜色,它将对我有所帮助。但是,如果我决定将风格完全改变为其他风格,如在绿色背景中呢?我将不得不改变所有定义的样式。 2.有些地方我不能使用这些定义的样式,因为触发器更复杂。例如,我有一个MultipleDataTrigger字段,它有多个要求使其成为强制性的,但它的风格应该与其他控件相同... – Rose

相关问题