Skip to content

CodeSnippet-WPF

约 2789 字大约 9 分钟

WPFCodeSnippet

2023-12-28

渐变色类

<Border.Background>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="{DynamicResource ContentAreaColorLight}" Offset="0" />
        <GradientStop Color="{DynamicResource ContentAreaColorDark}" Offset="1" />
    </LinearGradientBrush>
</Border.Background>

<Setter Property="Background">
    <Setter.Value>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="White" Offset="0"/>
            <GradientStop Color="Yellow" Offset="0.5"/>
            <GradientStop Color="White" Offset="1"/>
        </LinearGradientBrush>
    </Setter.Value>
</Setter>

<!--渐变三角--
<Path Width="14" Height="32" Stretch="Uniform" Fill="Red" Stroke="Transparent" Data="M0,16 L14,0 L14,32 Z"/>
<Path Width="14" Height="32" Stretch="Uniform" Stroke="Transparent" Data="M0,16 L14,0 L14,32 Z" Margin="20">
    <Path.Fill>
        <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
            <GradientStop Offset="0" Color="Red" />
            <GradientStop Offset="1" Color="Yellow" />
        </LinearGradientBrush>
    </Path.Fill>
</Path>

ai

透明度结合变换

<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Grid.IsSharedSizeScope="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" SharedSizeGroup="Row" />
        <RowDefinition SharedSizeGroup="Row" />
        <!--  SharedSizeGroup="Row" 共享行高度  -->
    </Grid.RowDefinitions>
    <TextBox x:Name="txt" Width="400" FontSize="26" Text="镜面反射" />
    <Rectangle Grid.Row="1" RenderTransformOrigin="1,0.5">
        <Rectangle.Fill>
            <VisualBrush Visual="{Binding ElementName=txt}" />
        </Rectangle.Fill>
        <Rectangle.OpacityMask>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Offset="0.2" Color="Transparent" />
                <GradientStop Offset="1" Color="#44000000" />
            </LinearGradientBrush>
        </Rectangle.OpacityMask>
        <Rectangle.RenderTransform>
            <ScaleTransform ScaleY="-1" />
        </Rectangle.RenderTransform>
    </Rectangle>
</Grid>

ai

自定义模板 动态属性替代不存在的属性 [Tag 替代 CornerRadius]

<Style x:Key="ImageRadioButtonStyle" TargetType="RadioButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RadioButton">
                <Grid>
                    <Border x:Name="PART_Border" Background="#212630" CornerRadius="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Tag}"/>
                    <Image x:Name="PART_Image" Width="45" Height="45" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="PART_Border" Property="Background" Value="#FF4398FF"/>
                        <Setter TargetName="PART_Border" Property="Opacity" Value="0.5"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<--使用-->
<RadioButton Content="/AutoImageAnalysis;component/Resources/Icon/Scan/focus1.png"
             Tag="8 8 0 0"/>

Path

<Path Stroke="Red" Fill="Transparent" Data="M 0,0 L 5,10 L11,0"/>
// Stroke:边框颜色
// Fill:内部填充颜色

Combobox MVVM使用事件监听触发器

MainWindow.xaml
<Window x:Class="BlankApp1.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:prism="http://prismlibrary.com/"
        Title="{Binding Title}"
        Width="525"
        Height="350"
        prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
            <ComboBox Name="ComboBox1"
                      Width="200"
                      Height="35"
                      DisplayMemberPath="Value"
                      ItemsSource="{Binding ComboBox1Items}"
                      SelectedItem="{Binding SelectItem1, Mode=TwoWay}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ComboBox}}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
    </Grid>
</Window>

TabControl

tabcontrol

基本样式 | 多条件

DataGrid

datagrid

datagrid1

依赖属性与命令

自定义控件

Pagination.xaml

前端使用

MainWindow.xaml
<df:Pagination CurrentPage="{Binding PageEntity.PageIndex}" Total="{Binding PageEntity.Total}" TotalPages="{Binding PageEntity.TotalPages}" PageUpdateCommand="{Binding PageUpdatCommand}" />

datagridpage

非按钮绑定命令

<StackPanel HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Cursor="Hand"
            Orientation="Vertical">
    <StackPanel.InputBindings>
          <MouseBinding Command="{Binding TobeMarkCommand}" MouseAction="LeftClick" />
    </StackPanel.InputBindings>
    <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
          <TextBlock VerticalAlignment="Bottom"
                    FontSize="60"
                    FontWeight="Bold"
                    Foreground="#363950"
                    Text="{Binding WorkbenchViewItems.UnmarkCount}" />
    </StackPanel>
</StackPanel>

绑定按键

<UserControl x:Class="MorphoScan.Modules.MainModule.Views.Login"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ex="clr-namespace:MorphoScan.Common.UiCore.Attach;assembly=MorphoScan.Common.UiCore"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <UserControl.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding LoginCommand}" />
    </UserControl.InputBindings>
    
    ...
    
    <Button Width="380"
                Height="51"
                Margin="100,85,0,0"
                Background="#0177FB"
                Command="{Binding LoginCommand}"
                Content="登录"
                Foreground="White" />
    
</UserControl>