2014-10-28 40 views
0

我想有幾個TextBlocks通常看起來相同,但每個需要在另一個觸發器上以另一種方式作出反應。我試過使用通用樣式(MyTextBlockStyle),稍後添加觸發器。但我得到的錯誤meesages像「財產」風格「已被宣佈兩次」或simillar。如何在幾個TextBlock控件上使用相同的樣式,但是每個控件都使用不同的綁定和觸發器?

爲了解釋我的意思,我用3個TextBlocks做了一個例子。其中2個綁定到每個不同的CheckBox,並且每個觸發不同的屬性(顯示的文本與前景色)。第三個TextBlock應根據TextbBox的內容更改其背景顏色。我怎樣才能實現這樣的事情?

<UserControl.Resources> 
     <Style x:Key="MyTextBlockStyle" TargetType="TextBlock"> 
      <Setter Property="Background" Value="Yellow"/> 
      <Setter Property="Foreground" Value="Black"/> 
      <Setter Property="Margin" Value="10"/> 
      <Setter Property="Padding" Value="5"/> 
     </Style> 
</UserControl.Resources> 

<Grid > 
     <StackPanel Margin="10"> 
      <CheckBox x:Name="CheckBox01" Content="Change Background of TextBlock 1" IsChecked="False" Foreground="White" Margin="5" /> 
      <CheckBox x:Name="CheckBox02" Content="Change Background of TextBlock 2" IsChecked="False" Foreground="White" Margin="5" /> 
      <TextBox x:Name="TextBox03" Padding="10" Background="White" Text="Enter Text here ..." Tooltip="Change Background of TextBlock 3"/> 
      <TextBlock Style="{StaticResource MyTextBlockStyle}" > 
       <TextBlock.Style> 
        <Style TargetType="TextBlock"> 
         <Setter Property="Text" Value="No" /> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=CheckBox01, Path=IsChecked}" Value="True"> 
           <Setter Property="Text" Value="Yes!" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBlock.Style> 
      </TextBlock> 
      <TextBlock Style="{Static Resource MyTextBlockStyle}" Text="Something different"> 
       <TextBlock.Style> 
        <Style TargetType="TextBlock"> 
         <Setter Property="Foreground" Value="Red" /> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=CheckBox02, Path=IsChecked}" Value="True"> 
           <Setter Property="Foreground" Value="Green" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBlock.Style> 
      </TextBlock> 
      <TextBlock Style="{Static Resource MyTextBlockStyle}" Text="Anything else"> 
       <TextBlock.Style> 
        <Style TargetType="TextBlock"> 
         <Setter Property="Background" Value="Yellow" /> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=TextBox03, Path=Text}" Value=""> 
           <Setter Property="Background" Value="Blue" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBlock.Style> 
      </TextBlock> 
     </StackPanel> 
</Grid > 

回答

0

請試試這個code.It works.I想,如你所願

<Window.Resources>  
    <Style TargetType="TextBlock">    
     <Style.Triggers> 
      <MultiDataTrigger> 
       <MultiDataTrigger.Conditions> 
        <Condition Binding="{Binding ElementName=CheckBox01,Path=IsChecked}" Value="True"/> 
        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="tb1"/> 
       </MultiDataTrigger.Conditions> 
       <Setter Property="TextBlock.Background" Value="Orange"/> 
      </MultiDataTrigger> 

      <MultiDataTrigger> 
       <MultiDataTrigger.Conditions> 
        <Condition Binding="{Binding ElementName=CheckBox02,Path=IsChecked}" Value="True"/> 
        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="tb2"/> 
       </MultiDataTrigger.Conditions> 
       <Setter Property="TextBlock.Background" Value="Pink"/> 
      </MultiDataTrigger> 

      <MultiDataTrigger> 
       <MultiDataTrigger.Conditions> 
        <Condition Binding="{Binding ElementName=TextBox03,Path=Text}" Value=""/> 
        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="tb3"/> 
       </MultiDataTrigger.Conditions> 
       <Setter Property="TextBlock.Background" Value="Green"/> 
      </MultiDataTrigger> 

     </Style.Triggers> 
    </Style> 
</Window.Resources> 

<Grid> 
    <StackPanel Margin="10">   
     <CheckBox x:Name="CheckBox01" Content="Change Background of TextBlock 1" IsChecked="False" Margin="5"/>   
     <CheckBox x:Name="CheckBox02" Content="Change Background of TextBlock 2" IsChecked="False" Margin="5"/>   
     <TextBox x:Name="TextBox03" Padding="10" Background="White" Text="Enter Text here ..."/>   
     <TextBlock Tag="tb1"/> 
     <TextBlock Tag="tb2" Text="Something different"/> 
     <TextBlock Tag="tb3" x:Name="tb3" Text="Anything else"/>    
    </StackPanel> 
</Grid > 
+0

是否只有使用樣式的方式呢?由於這些觸發器每個都是相當獨特的,我希望能夠直接在控件內直接「修改」樣式,如果不使用'style ='屬性,就可能會這樣。無論如何,感謝您的解決方案。 – Wolfgang 2014-10-31 13:14:11

0

您正在尋找支持算法FMP屬性。它總是讓我感覺到。

<TextBlock> 
      <TextBlock.Style> 
       <Style TargetType="TextBlock" BasedOn="MyTextBlockStyle"> 
        <Setter Property="Text" Value="No" /> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding ElementName=CheckBox01, Path=IsChecked}" Value="True"> 
          <Setter Property="Text" Value="Yes!" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </TextBlock.Style> 
     </TextBlock> 
相關問題