WPF 自定义标题栏 自定义菜单栏

自定义标题栏

自定义列表,可以直接修改WPF中的ListBox模板,也用这样类似的效果。但是ListBox是不能设置默认选中状态的。

而我们需要一些复杂的UI效果,还是直接自定义控件来的快

 WPF 自定义标题栏 自定义菜单栏

GitHub下载地址:https://github.com/Kybs0/MenuListControl

一、设计界面样式

<UserControl x:Class="WpfApplication6.TitleListControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="800" Loaded="TitleListControl_OnLoaded" >
    <UserControl.Resources>
        <Style x:Key="FirstButtonStyle" TargetType="RadioButton">
            <Setter Property="Margin" Value="0.5,2"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <Grid>
                            <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E" CornerRadius="15,0,0,15"></Border>
                            <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="RadioButton">
                <Setter Property="Margin" Value="0.5,2"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type RadioButton}">
                            <Grid>
                                <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E"></Border>
                                <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        <Style x:Key="LastButtonStyle" TargetType="RadioButton">
                <Setter Property="Margin" Value="0.5,2"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type RadioButton}">
                            <Grid>
                                <Border x:Name="ButtonBorder" Height="35" Width="100" Background="#FF286E9E" CornerRadius="0,15,15,0"></Border>
                                <TextBlock Text="{TemplateBinding Content}" Foreground="White" FontSize="18" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="ButtonBorder" Property="Background" Value="DeepSkyBlue"></Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    </UserControl.Resources>
    <Grid>
        <Border x:Name="ControlBorder" VerticalAlignment="Center" HorizontalAlignment="Center" CornerRadius="16,16,16,16">
            <Border.Background>
                <LinearGradientBrush StartPoint="0,1" EndPoint="1,1">
                    <GradientStop Color="White" Offset="0.2"></GradientStop>
                    <GradientStop Color="DeepSkyBlue" Offset="1"></GradientStop>
                </LinearGradientBrush>
            </Border.Background>
            <StackPanel x:Name="SpTitleList" Orientation="Horizontal" Background="Transparent" Margin="2,0">
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
View Code

相关文章: