簡體   English   中英

如何在 ToggleButton.IsChecked 或 IsPressed 上正確使用觸發器?

[英]How to use a trigger on ToggleButton.IsChecked or IsPressed correctly?

那是我的代碼:

<Window x:Class="WpfTests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Thickness x:Key="tasksMargin">0,10,0,0</Thickness>

</Window.Resources>
<Grid>
    <DockPanel HorizontalAlignment="Left">
        <!--<Border Padding="15">-->
        <GroupBox>
            <GroupBox.Header>
                Database Tasks
            </GroupBox.Header>
            <StackPanel Width="Auto" >
                <StackPanel.Resources>
                    <Style  TargetType="RadioButton">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="RadioButton">
                                    <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                      Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                  BorderThickness="2" BorderBrush="Yellow" Background="White"/>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                                                <Setter Property="Content" Value="different txt" />
                                            </Trigger>
                                        <Trigger Property="ToggleButton.IsPressed" Value="True">
                                                <Setter Property="Content" Value="different text" />
                                            </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Margin" Value="5">
                        </Setter>
                    </Style>
                </StackPanel.Resources>

                <RadioButton  GroupName="Group1"  x:Name="button1">Option1</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button2" x:Name="button2">button2</RadioButton>
                <RadioButton  GroupName="Group1" x:Uid="button3" x:Name="button3">button3</RadioButton>
            </StackPanel>
        </GroupBox>
    </DockPanel>
</Grid>

但是,不會觸發 IsChecked 和 IsPressed 觸發器。 我如何正確聲明它們?

如果你給你的 ToggleButton 一個名字,然后在觸發器設置器中引用它,那么它應該可以工作。 所以,你的 ControlTemplate 看起來像......

<ControlTemplate TargetType="RadioButton">
    <ToggleButton Name="toggleButton" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                  Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                              BorderThickness="2" BorderBrush="Yellow" Background="White"/>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsChecked" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different txt" />
        </Trigger>
        <Trigger Property="ToggleButton.IsPressed" Value="True">
            <Setter TargetName="toggleButton" Property="Content" Value="different text" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

不需要觸發器中的前綴類型,還將切換按鈕上的綁定更改為TemplateBindings

還有一些其他的解決方案可用,但對於這個問題。 請執行下列操作:

1) 提供您的切換名稱

<ToggleButton Name="tglBtn" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
              BorderThickness="2" BorderBrush="Yellow" Background="White"/>

2) 在給定名稱的觸發器引用中

<Trigger SourceName="tglBtn" Property="IsChecked" Value="True">
      <Setter TargetName="tglBtn" Property="Content" Value="different txt" />
</Trigger>

希望有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM