gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:20:30 26 4
gpt4 key购买 nike

我的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com