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

Button.IsSelected WPF MVVM中的属性类型?,wpf,mvvm,button,menu,Wpf,Mvvm,Button,Menu,我的WPF应用程序中有几个按钮作为菜单 此场景类似于网站中的菜单 当我单击其中一个按钮时,我希望该按钮样式与其他按钮样式不同,当我选择另一个按钮时,前一个按钮应为普通按钮,并且selectedstyle应应用于此选定按钮 您能告诉我如何通过ControlTemplate实现这一点,或者我必须维护一个IsSelected属性,让我们知道选择了哪个按钮吗 谢谢 VJ您应该使用内置的可视状态处理,并在XAML中创建状态/样式 在您的具体案例中,您所追求的似乎是一组单选按钮,这样您就不必编写任何代码来处

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

此场景类似于网站中的菜单

当我单击其中一个按钮时,我希望该按钮样式与其他按钮样式不同,当我选择另一个按钮时,前一个按钮应为普通按钮,并且selectedstyle应应用于此选定按钮

您能告诉我如何通过ControlTemplate实现这一点,或者我必须维护一个IsSelected属性,让我们知道选择了哪个按钮吗

谢谢


VJ

您应该使用内置的可视状态处理,并在XAML中创建状态/样式


在您的具体案例中,您所追求的似乎是一组单选按钮,这样您就不必编写任何代码来处理状态之间的切换。

您应该使用内置的可视状态处理,并在XAML中创建状态/样式


在您的具体案例中,您所追求的似乎是一组单选按钮,这样您就不必编写任何代码来处理状态之间的切换。

为此,您最好使用自定义单选按钮列表。请查看下面的帖子


您可以轻松自定义选定和取消选定的样式以符合您的要求

为此,您最好使用自定义单选按钮列表。请查看下面的帖子


您可以轻松自定义选定和取消选定的样式以符合您的要求

您可以使用Radio按钮进行尝试。下面的示例将为RadioButton创建一个平面按钮外观

<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>

对于带有图像的RadioButton,请查看Matt的博客


您可以尝试使用Radio按钮。下面的示例将为RadioButton创建一个平面按钮外观

<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>

对于带有图像的RadioButton,请查看Matt的博客


如何将单选按钮设计成按钮?使用切换按钮如何。我能从中获得同样的效果吗?你们有Expression Blend吗?使用Blend最容易做到这一点,您可以添加一个RadioButton>右键单击>编辑模板>编辑副本…,这将为您生成默认的控制模板,可以自定义为按钮(甚至是切换按钮)是的,您可以使用切换按钮,但是,当你按下下一个按钮时,你必须进行一些手动接线,使上一个按钮本身“不可切换”,使用单选按钮并对它们进行分组意味着这已经解决了。谢谢。你的答案也是一个答案。但只能标记1个答案。我们如何将单选按钮样式化为按钮?使用切换按钮如何。我能从中获得同样的效果吗?你们有Expression Blend吗?使用Blend最容易做到这一点,您可以添加一个RadioButton>右键单击>编辑模板>编辑副本…,这将为您生成默认的控制模板,可以自定义为按钮(甚至是切换按钮)是的,您可以使用切换按钮,但是,当你按下下一个按钮时,你必须进行一些手动接线,使上一个按钮本身“不可切换”,使用单选按钮并对它们进行分组意味着这已经解决了。谢谢。你的答案也是一个答案。但只能标记1个答案。