WPF의 사용자 컨트롤 및 사용자 지정 컨트롤

WPF의 사용자 컨트롤 및 사용자 지정 컨트롤

WPF 또는 WinForm에는 사용자 컨트롤(UserControl)과 사용자 지정 컨트롤(CustomControl)이 있으며, 이 두 컨트롤은 기능 재사용을 위해 기존 컨트롤을 캡슐화한 것입니다. 그러나 둘 사이에는 여전히 몇 가지 차이점이 있습니다. 이 문서에서는 이러한 두 컨트롤에 대해 설명합니다.

  1. 사용자 제어
    • 복합 컨트롤 사용에 주의하세요. 즉, 여러 기존 컨트롤이 재사용 가능한 컨트롤 그룹을 형성합니다.
    • XAML과 백그라운드 코드로 구성되어 단단히 결합됨
    • 템플릿 재작성은 지원되지 않습니다.
    • UserControl에서 상속됨
  2. 맞춤 제어
    • 기능 확장을 위해 기존 컨트롤을 상속하고 새로운 기능을 추가하는 등 컨트롤을 직접 구현하십시오.
    • 백그라운드 코드와 Generic.xaml의 조합
    • 사용 시 템플릿 재작성 지원
    • Control에서 상속됨

사용자 제어

RelativeSource사용자 컨트롤은 일반적인 WPF 형식과 유사하게 상대적으로 이해하기 쉽습니다. 바인딩을 내부적으로 사용할 때 좋은 캡슐화를 달성하는 방식으로 바인딩 해야 한다는 점은 주목할 가치가 있습니다 . 간단한 경우:

  • 사용자 컨트롤 정의
<UserControl
    x:Class="WpfApp19.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp19"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid>
        <!--下面绑定都要用RelativeSource作为源-->
        <StackPanel>
            <TextBox
                Width="100"
                BorderThickness="2"
                Text="{Binding value, RelativeSource={RelativeSource AncestorType=UserControl}}" />
            <Button
                Width="100"
                Command="{Binding Command, RelativeSource={RelativeSource AncestorType=UserControl}}"
                Content="Ok" />
        </StackPanel>
    </Grid>
</UserControl>

뒤에 코드

//根据需要定义依赖属性 
//所需要绑定的值
 public int value
 {
    
    
     get {
    
     return (int)GetValue(valueProperty); }
     set {
    
     SetValue(valueProperty, value); }
 }
 public static readonly DependencyProperty valueProperty =
     DependencyProperty.Register("value", typeof(int), typeof(UserControl1), new PropertyMetadata(0));

 //所需要绑定的命令
 public ICommand Command
 {
    
    
     get {
    
     return (ICommand)GetValue(CommandProperty); }
     set {
    
     SetValue(CommandProperty, value); }
 }
 public static readonly DependencyProperty CommandProperty =
     DependencyProperty.Register("Command", typeof(ICommand), typeof(UserControl1), new PropertyMetadata(default(ICommand)));


 //所需要绑定的命令参数
 public object CommandParemeter
 {
    
    
     get {
    
     return (object)GetValue(CommandParemeterProperty); }
     set {
    
     SetValue(CommandParemeterProperty, value); }
 }
 public static readonly DependencyProperty CommandParemeterProperty =
     DependencyProperty.Register("CommandParemeter", typeof(object), typeof(UserControl1), new PropertyMetadata(0));
  • 사용자 컨트롤 사용
<Window x:Class="WpfApp19.MainWindow"
        ...
        xmlns:local="clr-namespace:WpfApp19"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:UserControl1 value="{Binding }" Command="{Binding }"/>
    </Grid>
</Window>

맞춤 제어

CustomControl1.cs사용자 지정 컨트롤 추가를 클릭하면 파일과 디렉터리가 추가되고 Themes디렉터리 아래에 사용자 지정 컨트롤의 스타일인 Generic.xaml파일이 있습니다. 编辑模板특정 제어 작업 에 대해 자주 생성하는 스타일은 创建副本실제로 Generic.xaml에 정의된 스타일입니다. 또한 때로는 동일한 소프트웨어가 다른 Windows 버전에서 실행되고 성능이 다를 수 있으며 일부 시스템도 실행할 수 없는 상황이 발생할 수 있습니다. 이는 다른 시스템에서 기본 테마와 다릅니다. 실제로 wpf 컨트롤이 사용자 지정 스타일을 찾을 수 없을 때 시스템에서 스타일을 가져옵니다.검색 순서는 먼저 자신이 위치한 어셈블리를 찾는 것입니다.어셈블리가 기능을 정의하면 속성 값을 확인합니다. 속성이 yes이면 특정 테마 리소스가 없음을 의미하고 값은 ThemeInfo특정 ThemeInfoDictionaryLocation리소스 가 어셈블리 내부에 정의되어 있음을 의미하며 값은 외부에 있음을 의미 합니다 . 실제로 시스템의 기본 스타일과 관련이 있습니다 .NoneSourceAssemblyExternalAssemblythemes/generic.xamlgeneric.xaml

다른 xaml에 해당하는 시스템 테마

이미지-20230208144759832

버튼 케이스

이미지

C# 파일

public class Switch : ToggleButton
{
    
    
    static Switch()
    {
    
    
        //通过重写Metadata,控件就会通过程序集themes文件夹下的generic.xaml来寻找系统默认样式
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Switch), new FrameworkPropertyMetadata(typeof(Switch)));
    }
}

Themes 폴더 아래의 Generic.xaml 파일

이 파일에는 중국어가 없어야 하며 주석도 없어야 합니다.

<Style TargetType="{x:Type local:Switch}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Switch}">
                <Grid>
                    <Border
                        Name="dropdown"
                        Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"
                        Margin="-23"
                        CornerRadius="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"
                        Visibility="Collapsed">
                        <Border.Background>
                            <RadialGradientBrush>
                                <GradientStop Offset="1" Color="Transparent" />
                                <GradientStop Offset="0.7" Color="#5500D787" />
                                <GradientStop Offset="0.59" Color="Transparent" />
                            </RadialGradientBrush>
                        </Border.Background>
                    </Border>
                    <Border
                        Name="bor"
                        Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"
                        Background="Gray"
                        BorderBrush="DarkGreen"
                        BorderThickness="5"
                        CornerRadius="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}">
                        <Border
                            Name="bor1"
                            Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}"
                            Margin="2"
                            Background="#FF00C88C"
                            CornerRadius="{Binding RelativeSource={RelativeSource Self}, Path=ActualHeight}" />
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation
                                        Storyboard.TargetName="bor1"
                                        Storyboard.TargetProperty="Background.Color"
                                        To="White"
                                        Duration="0:0:0.5" />
                                    <ColorAnimation
                                        Storyboard.TargetName="bor"
                                        Storyboard.TargetProperty="BorderBrush.Color"
                                        To="#FF32FAC8"
                                        Duration="0:0:0.5" />
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="dropdown" Storyboard.TargetProperty="Visibil
                                        <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="bor1" Storyboard.TargetProperty="Background.Color" />
                                    <ColorAnimation Storyboard.TargetName="bor" Storyboard.TargetProperty="BorderBrush.Color" />
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="dropdown" Storyboard.TargetProperty="Visibil
                                        <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Collapsed</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

사용자 지정 컨트롤 사용

<Grid>
    <local:Switch Width="100" Height="100" />
</Grid>

사용자 지정 컨트롤에서 일반적으로 사용되는 지식 포인트

  1. TemplatePart 속성

사용자 지정 컨트롤에서 일부 컨트롤에는 다시 작성된 OnApplyTemplate() 메서드에서 지정된 버튼을 가져오는 것과 같이 쉽게 호출할 수 있는 이름이 필요합니다. 이를 위해서는 사용자가 컨트롤을 사용할 때 템플릿의 이름을 수정할 수 없어야 합니다.

//应用该控件时调用
public override void OnApplyTemplate()
{
    
    
    UpButtonElement = GetTemplateChild("UpButton") as RepeatButton;
    DownButtonElement = GetTemplateChild("DownButton") as RepeatButton;
}

따라서 클래스 앞에 속성을 사용하여 표시

[TemplatePart(Name = "UpButton", Type = typeof(RepeatButton))]
[TemplatePart(Name = "DownButton", Type = typeof(RepeatButton))]
public class Numeric : Control{
    
    }
  1. 시각적 상태의 정의 및 호출

사용자 지정 컨트롤에서 시각적 상태를 정의하여 효과를 다른 상태로 표시할 수 있습니다.

xml에서 시각적 상태를 정의하고 다른 그룹 아래의 시각적 상태는 상호 배타적입니다.

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup Name="FocusStates">
        <VisualState Name="Focused">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisual" 
                           Storyboard.TargetProperty="Visibility" Duration="0">
                    <DiscreteObjectKeyFrame KeyTime="0">
                        <DiscreteObjectKeyFrame.Value>
                            <Visibility>Visible</Visibility>
                        </DiscreteObjectKeyFrame.Value>
                    </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
        <VisualState Name="Unfocused"/>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

C#의 해당 시각적 상태 전환

private void UpdateStates(bool useTransitions)
{
    
    
    if (IsFocused)
    {
    
    
        VisualStateManager.GoToState(this, "Focused", false);
    }
    else
    {
    
    
        VisualStateManager.GoToState(this, "Unfocused", false);
    }
}

동시에 특성을 사용하여 백그라운드 클래스에 표시할 수 있습니다.

[TemplateVisualState(Name = "Focused", GroupName = "FocusedStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusedStates")]
public class Numeric : Control{
    
    }

실제로 Trigger를 사용하여 이 기능을 달성할 수 있습니다.

사례 연구 소스 코드 다운로드

사용자 컨트롤 및 사용자 지정 컨트롤의 여러 사례를 공유하면 효과는 다음과 같습니다.

이미지

소스 코드 다운로드

추천

출처blog.csdn.net/weixin_44064908/article/details/128938443