如何添加一个“;“鼠标点击”;WPF样式的行为
所以我有这个WPF风格:如何添加一个“;“鼠标点击”;WPF样式的行为,wpf,styles,Wpf,Styles,所以我有这个WPF风格: <Style x:Key="SmallLinkButton" TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <TextBlock TextDecorations="Underline">
<Style x:Key="SmallLinkButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock TextDecorations="Underline">
<ContentPresenter />
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#234D20" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="#77AB59" />
</Trigger>
</Style.Triggers>
</Style>
若要制作链接按钮,请在鼠标悬停时切换其颜色。我有一套这种风格的按钮,我希望在用户点击鼠标后,能够将按钮前景更改为第二个值,除非他点击另一个按钮
仅仅通过使用样式就可以做到这一点吗?我不相信,但我是WPF的新手,或者,您将如何实现此功能
提前谢谢
解决方案
正如@Phil所建议的,解决方案是使用一个容器,在本例中是一个列表框,样式如下:
<!-- Start of the menu -->
<!-- Horizontal listbox-->
<Style x:Key="MenuListBox" TargetType="ListBox">
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Listbox item with the special behavior -->
<Style x:Key="MenuListBoxItem" TargetType="ListBoxItem">
<Style.Resources>
<!-- SelectedItem with focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<!-- SelectedItem without focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
<!-- SelectedItem text foreground -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="{DynamicResource {x:Static SystemColors.ControlTextColorKey}}" />
</Style.Resources>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
<!-- Menu buttons -->
<Style x:Key="BigMenuLinkButton" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe UI Light"/>
<Setter Property="FontSize" Value="36" />
<Setter Property="Foreground" Value="#C9DF8A" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="#77AB59" />
</Trigger>
<DataTrigger Value="True" Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}">
<Setter Property="Foreground" Value="#234D20"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="MediumMenuLinkButton" TargetType="TextBlock">
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="#C9DF8A" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="#77AB59" />
</Trigger>
<DataTrigger Value="True" Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}">
<Setter Property="Foreground" Value="#234D20"/>
</DataTrigger>
</Style.Triggers>
</Style>
<!-- End of the menu -->
以下是一个让您开始的想法 设置列表框的样式,使列表框项目看起来像链接按钮: Xaml
C#
公共类链接按钮InListBoxViewModel
{
公共链接按钮InListBoxViewModel()
{
项目=新列表{“一”、“二”、“三”};
}
公共列表项{get;private set;}
}
您是否尝试过使用togglebutton?它已显示event.Nop。我没有试过。但是威尔和评论,谢谢你的提示。你是在寻找单选按钮式的群体行为吗?一个按钮总是被“选中”吗?@Phil这正是我要找的!绝对精彩。谢谢,我只是编辑我的问题来反映你的想法。
<Page.Resources>
<Style x:Key="SmallLinkButton" TargetType="TextBlock">
<Setter Property="TextDecorations" Value="Underline"/>
<Setter Property="Foreground" Value="#234D20" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="#77AB59" />
</Trigger>
<DataTrigger Value="True"
Binding="{Binding Path=IsSelected,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ListBoxItem}}}">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="NullSelectionListBoxItem" TargetType="ListBoxItem">
<Style.Resources>
<!-- SelectedItem with focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<!-- SelectedItem without focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
<!-- SelectedItem text foreground -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="{DynamicResource {x:Static SystemColors.ControlTextColorKey}}" />
</Style.Resources>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</Page.Resources>
<Page.DataContext>
<Samples:LinkButtonsInListBoxViewModel/>
</Page.DataContext>
<Grid>
<ListBox ItemsSource="{Binding Items}"
ItemContainerStyle="{StaticResource NullSelectionListBoxItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Margin="5"
Style="{StaticResource SmallLinkButton}" Text="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
public class LinkButtonsInListBoxViewModel
{
public LinkButtonsInListBoxViewModel()
{
Items = new List<string> {"One", "Two", "Three"};
}
public List<string> Items { get; private set; }
}