我创建了一个自定义WPF控件,样式似乎并没有起作用。我尝试过创建外部样式和内部样式,但似乎都不起作用。下面是内部风格的代码。
自定义控制
public class NavigationTextBlock : TextBlock
{
static NavigationTextBlock()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(NavigationTextBlock),
new FrameworkPropertyMetadata(typeof(NavigationTextBlock)));
}
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool), typeof(NavigationTextBlock), new UIPropertyMetadata(false));
public bool IsSelected
{
get
{
return (bool)GetValue(IsSelectedProperty);
}
set
{
SetValue(IsSelectedProperty, value);
}
}
}
XAML
<c:NavigationTextBlock.Styles>
下的任何样式均未应用。
<UserControl x:Class="Fallout4Checklist.Views.NavigationView"
xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Fallout4Checklist.Views"
xmlns:c="clr-namespace:Fallout4Checklist.Controls"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d"
d:DesignHeight="100"
d:DesignWidth="800">
<Border Padding="16, 0, 16, 0">
<ItemsControl ItemsSource="{Binding NavigationItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<c:NavigationTextBlock
FontFamily="Segoe UI"
FontSize="24"
FontWeight="SemiBold"
Foreground="DarkGray"
VerticalAlignment="Center"
TextAlignment="{Binding TextAlignment}"
Text="{Binding Content}">
<c:NavigationTextBlock.Style>
<Style TargetType="{x:Type c:NavigationTextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver}" Value="True">
<Setter Property="Foreground" Value="#CC119EDA" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSelected}" Value="False">
<Setter Property="Foreground" Value="DarkGray" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#0d78a6" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected}" Value="False" />
<Condition Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="#8c8c8c" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</c:NavigationTextBlock.Style>
</c:NavigationTextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</UserControl>
答案 0 :(得分:1)
您正在将属性直接设置到控件,这将覆盖样式。
删除
Foreground="DarkGray"