2016-02-15 69 views
0

我有一個ToggleButton,它有一個標籤。當它被選中的標籤文本的變化和變長,且字號變小:WPF ToggleButton與檢查+按下狀態

<Style x:Key="MyToggleButtonStyle" TargetType="{x:Type ToggleButton}"> 
    <Setter Property="FontSize" Value="22" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ToggleButton}"> 
       <Grid> 
        <Border Background="#FF0000"/> 
        <ContentPresenter x:Name="content"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsChecked" Value="True"> 
         <Setter Property="FontSize" Value="17" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<ToggleButton {StaticResource MyToggleButtonStyle}" Click="ToggleButton_Click"> 
    <TextBlock x:Name="ToggleButtonLabel" Text="Some Text"/> 
</ToggleButton> 


private void ToggleButton_Click(object sender, RoutedEventArgs e) { 
    ToggleButtonLabel.Text = ((ToggleButton)sender).IsChecked == false ? "Some Text" : "Some Longer Text"; 
} 

的按鈕也需要有一個壓狀態,其中字號降低相對於它的當前字號。換句話說,它應該這樣工作:

正常:字體大小= 22

壓:字體大小= 20

選中:字體大小= 17

經過+按下: Fontsize = 15

我知道我可以很容易地使用一個觸發IsPressed國家,但是這也將改變爲經過+的追問下,這不是我想要的:

<Trigger Property="IsPressed" Value="True"> 
    <Setter Property="FontSize" Value="20" /> 
</Trigger> 

那麼,什麼是添加第四視覺狀態結合的最簡單,最徹底的方法Checked + Pressed?

回答

0

你可以從multitriggers做到這一點很容易:

<Style x:Key="MyToggleButtonStyle" TargetType="{x:Type ToggleButton}"> 
    <Setter Property="FontSize" Value="22" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ToggleButton}"> 
       <Grid> 
        <Border Background="#FF0000" /> 
        <ContentPresenter x:Name="content" /> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsPressed" Value="True" /> 
          <Condition Property="IsChecked" Value="False" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="FontSize" Value="20" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsPressed" Value="False" /> 
          <Condition Property="IsChecked" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="FontSize" Value="17" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsPressed" Value="True" /> 
          <Condition Property="IsChecked" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter Property="FontSize" Value="15" /> 
        </MultiTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<ToggleButton Style="{StaticResource MyToggleButtonStyle}" Click="ToggleButton_Click"> 
    <TextBlock x:Name="ToggleButtonLabel" Text="Some Text" /> 
</ToggleButton> 
+0

完美。謝謝 – Karmacon