1

静态资源样式

<Style TargetType="{x:Type ToggleButton}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ToggleButton">
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Background" Value="Red"></Setter>
                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter  Property="Background" Value="Green" />
                        </Trigger>
                    </Style.Triggers>
                </Style>

切换按钮代码

<ToggleButton  Grid.Row="3" Grid.Column="1" ToolTip="Toggle to Show and Hide Date" IsChecked="True" Cursor="Hand">
                <ToggleButton.Style>
                    <Style TargetType="{StaticResource ToggleButton}">
                    <Setter Property="Content" Value="No Date" />

                    <Style.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter  Property="Content" Value="Date" />
                        </Trigger>
                    </Style.Triggers>
                    </Style>
                </ToggleButton.Style>
            </ToggleButton>

但我无法将内容获取错误设置为Content is not recognized or inaccessible.

我不是 WPF 的常客。

谢谢

4

1 回答 1

3

In your Toggle Button Code, Change

<Style TargetType="{StaticResource ToggleButton}">

to

<Style TargetType="{x:Type ToggleButton}"  BasedOn="{StaticResource {x:Type ToggleButton}}">

You can have a style defined for a control globally that is/may apply to all controls of that type, but when you have to give individual control some extra styling you can do that by creating a style within the control and base that style on the global style.

This basedOn can be done on style x:Type (as in my answer), or can be based on x:Name as well if you want to base it on a specific style.

于 2015-04-20T08:17:53.703 回答