wpf - Button.WPF MVVM中的属性的选定类型吗?

标签 wpf mvvm button menu

我的WPF应用程序中的菜单按钮很少。

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

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

您能告诉我如何通过ControlTemplate实现此功能,还是必须维护一个IsSelected属性,使我们知道选择了哪个按钮?

谢谢,

VJ

最佳答案

您可以尝试使用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>

用于带图像的RadioButton的内容,请查看Matt的博客

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

关于wpf - Button.WPF MVVM中的属性的选定类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6186489/

相关文章:

wpf - 在 MouseOver 上更改用作按钮模板的路径的填充颜色

.net - 如何禁用 WPF WebBrowser 控件的点击噪音?

c# - EventToCommand 无法添加到 TriggerActionCollection

c# - 如何将 ListView 项上下文菜单绑定(bind)到 ICommand

c# - 如何使用 nameof 而不是魔术字符串来实现 INotifyPropertyChanged?

delphi按钮第二次移动时消失

javascript - 单击页面加载时的按钮元素

c# - WPF MVVM DataBindings 停止更新

android - Android 中按钮(资源处理)的状态维护?

c# - 如何检查线程是否完成,然后在 C#/WPF 中填充进度条