wpf样式学习
学习大佬视频地址:https://www.bilibili.com/video/BV1nY411a7T8/?p=58&spm_id_from=333.788.top_right_bar_window_history.content.click&vd_source=a4e06be300e655612460fd5149552558
MaterialDesign,HandyControl
wpf样式加载
窗体加载
<Window.Resources> <Style TargetType="Button"> <Setter Property="Background" Value="Blue"/> <Setter Property="Foreground" Value="White"/> </Style> </Window.Resources>
app加载
写样式文件,或者直接在app中加载
//样式文件 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <materialDesign:BundledTheme BaseTheme="Dark" PrimaryColor="DeepPurple" SecondaryColor="Lime" /> //地址加载(可以多个) <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> </ResourceDictionary.MergedDictionaries> //直接写样式 <Style x:Key="MyListBoxItemStyle" TargetType="ListBoxItem"> <Setter Property="MinHeight" Value="40" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Grid> <Border x:Name="borderHeader" /> <Border x:Name="border" /> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="borderHeader" Property="BorderThickness" Value="4,0,0,0" /> <Setter TargetName="borderHeader" Property="BorderBrush" Value="{DynamicResource PrimaryHueLightBrush}" /> <Setter TargetName="border" Property="Background" Value="{DynamicResource PrimaryHueLightBrush}" /> <Setter TargetName="border" Property="Opacity" Value="0.2" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="{DynamicResource PrimaryHueLightBrush}" /> <Setter TargetName="border" Property="Opacity" Value="0.2" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> </Application.Resources>
样式的重点
ContentPresenter:动态绑定内容(<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />)
RelativeSource:找数据来源(<MouseBinding MouseAction="LeftClick"
Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}"
CommandParameter="{Binding}"/>)
CommandParameter:传递参数({Binding ElementName=todoList,Path=SelectedItem})
Converter:参数值转换( IsChecked="{Binding Status,Converter={StaticResource intToBool}}")
自定义控件
定义绑定值
public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(DataBlock), new PropertyMetadata(0.0));
datatemplate模板
<GroupBox Header="文本发送日志"> <ScrollViewer VerticalScrollBarVisibility="Hidden"> <ItemsControl ItemsSource="{Binding LogList}"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Height="30"> <Grid.ColumnDefinitions> <ColumnDefinition Width="20"/> <ColumnDefinition Width="auto"/> <ColumnDefinition/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <Border Width="8" Height="8" CornerRadius="4" Background="Orange"/> <TextBlock Grid.Column="1" VerticalAlignment="Center" Margin="5,0"> <Hyperlink TextDecorations="None" Foreground="#409EFE" Command="{Binding DataContext.ResendTextCommand,RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="{Binding LogInfo}">[重发]</Hyperlink> </TextBlock> <TextBlock Text="{Binding LogInfo}" Grid.Column="2" VerticalAlignment="Center" Foreground="#9FFF" TextTrimming="CharacterEllipsis" ToolTip="{Binding LogInfo}" FontWeight="ExtraLight" FontFamily="Microsoft YaHei"/> <TextBlock Text="{Binding LogTime,StringFormat=HH:mm:ss}" Grid.Column="3" FontSize="11" Foreground="#2FFF" VerticalAlignment="Center" Margin="5,0"/> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> </GroupBox>