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

发布于 2024-11-10 15:14:34 字数 231 浏览 2 评论 0原文

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

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

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

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

谢谢,

VJ

I have few buttons as Menu in my WPF App.

This scenario is something like menu in a website.

When I click one of the button, I want that button style to be different from others, and when I select another, the previous one should be normal and a selectedstyle should apply on this selected button.

Can you tell me how can I achieve this through ControlTemplate or do I have to maintain a IsSelected property that let us know which button is selected?

Thanks,

VJ

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶茶白久 2024-11-17 15:14:34

您可以尝试使用 RadionButton。下面的示例将为 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 的博客

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

You can try with the RadionButton. The below sample will create a Flat button look for the 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>

for RadioButton with Image just have look at Matt's blog

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

淡淡離愁欲言轉身 2024-11-17 15:14:34

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

在您的具体案例中,您似乎需要的是一组RadioButtons,这样您就不必编写任何代码来处理状态之间的切换。

You should use the built in visual state handling and create the states / styles in XAML.

In your concrete case it seems that what you're after is a group of RadioButtons, so that you don't have to write any code to deal with switching between states.

再浓的妆也掩不了殇 2024-11-17 15:14:34

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

不允许在列表框中取消选择/取消选择

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

You better Use a Custom RadioButton List for this purpose.Check out the below post.

Do not allow unselect/deselect in ListBox

You can easily customize the selected and deselected styles to match your requirement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文