在切换按钮内设置文本块的内容

时间:2016-06-10 03:37:57

标签: c# .net wpf

我试图在切换按钮的IsChecked属性发生变化时设置文本块的内容(如下所示)。我尝试了多种方法,但无法让它发挥作用。

<ToggleButton VerticalAlignment="Center" Margin="4 0 0 0" Background="{DynamicResource AccentColorBrush}" Name="toggle">
    <StackPanel Orientation="Horizontal">
        <Rectangle Width="20" Height="20" Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_starwars_jedi}" />
            </Rectangle.OpacityMask>
        </Rectangle>
        <TextBlock x:Name="SimulationToggleButtonText" Margin="4 0 0 0" VerticalAlignment="Center" Text="Start Simulation">
        </TextBlock>
    </StackPanel>
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Foreground" Value="Green"/>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content"  Value="Stop Simulation"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

1 个答案:

答案 0 :(得分:0)

您需要使用控件模板,请参阅此调整后的代码:

<ToggleButton VerticalAlignment="Center" Margin="4 0 0 0" Background="{DynamicResource AccentColorBrush}" Name="toggle">
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Foreground" Value="Green"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="20" Height="20" Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_starwars_jedi}" />
                                </Rectangle.OpacityMask>
                            </Rectangle>
                            <TextBlock x:Name="SimulationToggleButtonText" Margin="4 0 0 0" VerticalAlignment="Center" Text="Start Simulation">
                            </TextBlock>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="SimulationToggleButtonText" Property="Text" Value="Stop Simulation"/>
                                <Setter TargetName="SimulationToggleButtonText" Property="Foreground" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

问题是您的ToggleButton样式无法访问属于ToggleButton内容的任何控件。通过将控件和触发器移动到模板,它们位于相同的范围内。