1.StaticResource和DynamicResource的区别。(留坑,有时间再补充)
2.RecognizesAccessKey。(留坑,有时间再补充)
3.设置界面默认焦点位置,比如将焦点设置为默认在用户名输入框usernameBox:FocusManager.FocusedElement="{Binding ElementName=usernameBox}
4.利用附加属性,扩展控件的属性,以便在重写控件的template里面使用。
比较常见的一个场景是,你有一个button,这个button的背景是图片,同时当鼠标进入时,是另外一张图片,这个时候就缺少两个属性,一个是BackImage,一个是BackImageEnter。你可以写一个附加属性类,如下:
internal class StyleExtension
{
public static ImageSource GetBackImageEnter(DependencyObject obj)
{
return (ImageSource)obj.GetValue(BackImageEnterProperty);
}
public static void SetBackImageEnter(DependencyObject obj, ImageSource value)
{
obj.SetValue(BackImageEnterProperty, value);
}
// Using a DependencyProperty as the backing store for BackImageEnter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackImageEnterProperty =
DependencyProperty.RegisterAttached("BackImageEnter", typeof(ImageSource), typeof(StyleExtension), new PropertyMetadata(null));
public static ImageSource GetBackImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(BackImageProperty);
}
public static void SetBackImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(BackImageProperty, value);
}
// Using a DependencyProperty as the backing store for BackImage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackImageProperty =
DependencyProperty.RegisterAttached("BackImage", typeof(ImageSource), typeof(StyleExtension), new PropertyMetadata(null));
public static SolidColorBrush GetBackColorEnter(DependencyObject obj)
{
return (SolidColorBrush)obj.GetValue(BackColorEnterProperty);
}
public static void SetBackColorEnter(DependencyObject obj, SolidColorBrush value)
{
obj.SetValue(BackColorEnterProperty, value);
}
// Using a DependencyProperty as the backing store for BackColorEnter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackColorEnterProperty =
DependencyProperty.RegisterAttached("BackColorEnter", typeof(SolidColorBrush), typeof(StyleExtension), new PropertyMetadata(null));
}
然后在界面上这么使用(注意添加extension引用):
<Button Grid.Column="1" Content="测试按钮" Style="{StaticResource MenuButtonStyle}"
extension:StyleExtension.BackImage="/Views;Component/Resources/nomal.png"
extension:StyleExtension.BackImageEnter="/Views;Component/Resources/pressed.png"
Command="{Binding ChangePageCommand}" CommandParameter="createFlight"/>
然后控件的style这么写:
<Style x:Key="MenuButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" CornerRadius="2" Background="Transparent" Padding="10,0">
<Grid Margin="1">
<BulletDecorator VerticalAlignment="Center">
<BulletDecorator.Bullet>
<Image x:Name="image" Source="{Binding (extension:StyleExtension.BackImage),RelativeSource={RelativeSource Mode=TemplatedParent}}" Stretch="None"/>
</BulletDecorator.Bullet>
<TextBlock x:Name="textblock" Text="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#9AA5C0" FontSize="13" FontFamily="{StaticResource MainFontFamily}"/>
</BulletDecorator>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Background" Value="#3479D3"/>
<Setter TargetName="image" Property="Source" Value="{Binding (extension:StyleExtension.BackImageEnter),RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
<Setter TargetName="textblock" Property="Foreground" Value="#FFFFFF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
5.所有控件的官方样式重写方法:Button Styles and Templates - WPF .NET Framework | Microsoft Docs
6.重写ScrollViewer样式会导致lis