在WPF MVVM中Button.IsSelected类型的属性?

Var*_*ain 1 wpf menu button mvvm

在我的WPF应用程序中,我有几个按钮作为菜单.

这种情况类似于网站中的菜单.

当我单击其中一个按钮时,我希望该按钮样式与其他按钮样式不同,当我选择另一个按钮样式时,前一个应该是正常的,并且选定的样式应该应用于此选定按钮.

你能告诉我如何通过ControlTemplate实现这一目标,还是必须维护一个让我们知道选择了哪个按钮的IsSelected属性?

谢谢,

VJ

Kis*_*mar 6

您可以尝试使用RadionButton.下面的示例将为RadioButton创建一个Flat按钮外观.

<Window x:Class="WpfApplication8.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="472">
<Window.Resources>
    <Style TargetType="{x:Type RadioButton}">
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="GroupName"
                Value="filter" />
        <Setter Property="IsTabStop"
                Value="False" />
        <Setter Property="VerticalAlignment"
                Value="Center" />
        <Setter Property="HorizontalAlignment"
                Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <ControlTemplate.Resources>

                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="VerticalAlignment"
                                    Value="Center" />
                            <Setter Property="HorizontalAlignment"
                                    Value="Center" />
                        </Style>

                    </ControlTemplate.Resources>
                    <Border x:Name="PART_border"
                            CornerRadius="2"
                            Margin="2"
                            Background="Transparent"
                            BorderThickness="1"
                            BorderBrush="{x:Static SystemColors.ControlDarkBrush}"
                            SnapsToDevicePixels="True">
                        <StackPanel Orientation="Horizontal"
                                    HorizontalAlignment="Center" VerticalAlignment="Center">
                            <ContentPresenter x:Name="PART_content" />
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked"
                                 Value="True">
                            <Setter TargetName="PART_content"
                                    Property="TextBlock.FontWeight"
                                    Value="Bold" />
                            <Setter TargetName="PART_border"
                                    Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint="0,0"
                                                         EndPoint="0,1">
                                        <GradientStop Color="Black"
                                                      Offset="0" />
                                        <GradientStop Color="white"
                                                      Offset="1" />
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Orientation="Horizontal" >
    <RadioButton Height="30" Width="100" Content="First"></RadioButton>
    <RadioButton Height="30"
                 Width="100"
                 Content="Second"></RadioButton>
    <RadioButton Height="30"
                 Width="100"
                 Content="First"></RadioButton>
        </StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)

对于RadioButton with Image,请看看Matt的博客

http://madprops.org/blog/wpf-killed-the-radiobutton-star/