国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

WPF自定義控件庫之Window窗口

這篇具有很好參考價(jià)值的文章主要介紹了WPF自定義控件庫之Window窗口。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

在WPF開發(fā)中,默認(rèn)控件的樣式常常無法滿足實(shí)際的應(yīng)用需求,我們通常都會(huì)采用引入第三方控件庫的方式來美化UI,使得應(yīng)用軟件的設(shè)計(jì)風(fēng)格更加統(tǒng)一。常用的WPF的UI控件庫主要有以下幾種,如:Modern UI for WPF,MaterialDesignInXamlToolkit,PanuonUI,Newbeecoder.UI,WPF UI ,AduSkin,Panuon.UI.SilverHandyControl,MahApps.Metro,Kino.Toolkit.Wpf,Xceed Extended WPF Toolkit?,Kino.Toolkit.Wpf,PP.Wpf,Adonis-ui,CC.WPFTools,CookPopularControl ,PropertyTools,RRQMSkin,Layui-WPF,Arthas-WPFUI,fruit-vent-design,F(xiàn)luent.Ribbon,DMSkin WPF,EASkins_WPF,Rubyer-WPF,WPFDevelopers.Minimal ,WPFDevelopers,DevExpress WPF UI Library,ReactiveUI,Telerik UI for WPF等等,面對(duì)如此之多的WPF 第三方UI控件庫,可謂各有特色,不一而同,對(duì)于具有選擇綜合癥的開發(fā)人員來說,真的很難抉擇。在實(shí)際工作中,軟件經(jīng)過統(tǒng)一的UI設(shè)計(jì)以后,和具體的業(yè)務(wù)緊密相連,往往這些通用的UI框架很難百分之百的契合,這時(shí)候,就需要我們自己去實(shí)現(xiàn)自定義控件【Custom Control】來解決。本文以自定義窗口為例,簡述如何通過自定義控件來擴(kuò)展功能和樣式,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正。

?

自定義控件

?

自定義控件Custom Control,通過集成現(xiàn)有控件(如:Button , Window等)進(jìn)行擴(kuò)展,或者繼承Control基類兩種方式,和用戶控件【User Control不太相同】,具體差異如下所示:

  • 用戶控件UserControl
    1. 注重復(fù)合控件的使用,也就是多個(gè)現(xiàn)有控件組成一個(gè)可復(fù)用的控件組
    2. XAML和后臺(tái)代碼組成,綁定緊密
    3. 不支持模板重寫
    4. 繼承自UserControl
  • 自定義控件CustomControl
    1. 完全自己實(shí)現(xiàn)一個(gè)控件,如繼承現(xiàn)有控件進(jìn)行功能擴(kuò)展,并添加新功能
    2. 后臺(tái)代碼和Generic.xaml進(jìn)行組合
    3. 在使用時(shí)支持模板重寫
    4. 繼承自Control

本文所講解的側(cè)重點(diǎn)為自定義控件,所以用戶控件不再過多闡述。

?

WPF實(shí)現(xiàn)自定義控件步驟

?

1. 創(chuàng)建控件庫

?

首先在解決方案中,創(chuàng)建一個(gè)WPF類庫項(xiàng)目【SmallSixUI.Templates】,作為控件庫,后續(xù)所有自定義控件,都可以在此項(xiàng)目中開發(fā)。并創(chuàng)建Controls,Styles,Themes,Utils等文件夾,分別存放控件類,樣式,主題,幫助類等內(nèi)容,作為控件庫的基礎(chǔ)結(jié)構(gòu),如下所示:

WPF自定義控件庫之Window窗口

?

2. 創(chuàng)建自定義控件

?

在Controls目錄中,創(chuàng)建自定義窗口AiWindow,繼承自Window類,即此類具有窗口的一切功能,并具有擴(kuò)展出來的自定義功能。在此自定義窗口中,我們可以自定義窗口標(biāo)題的字體顏色HeaderForeground,背景色HeaderBackground,是否顯示標(biāo)題IsShowHeader,標(biāo)題高度HeaderHeight,窗口動(dòng)畫類型Type等內(nèi)容,具體如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation.Text;
using System.Windows;
using SmallSixUI.Templates.Utils;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shell;

namespace SmallSixUI.Templates.Controls
{
    [TemplatePart(Name = "PART_CloseWindowButton", Type = typeof(Button))]
    [TemplatePart(Name = "PART_MaxWindowButton", Type = typeof(Button))]
    [TemplatePart(Name = "PART_MinWindowButton", Type = typeof(Button))]
    public class AiWindow : Window
    {
        /// <summary>
        /// 關(guān)閉窗體
        /// </summary>
        private Button PART_CloseWindowButton = null;
        /// <summary>
        /// 窗體縮放
        /// </summary>
        private Button PART_MaxWindowButton = null;
        /// <summary>
        /// 最小化窗體
        /// </summary>
        private Button PART_MinWindowButton = null;

        /// <summary>
        /// 設(shè)置重寫默認(rèn)樣式
        /// </summary>
        static AiWindow()
        {
            StyleProperty.OverrideMetadata(typeof(AiWindow), new FrameworkPropertyMetadata(AiResourceHelper.GetStyle(nameof(AiWindow) + "Style")));
        }


        /// <summary>
        /// 頂部內(nèi)容
        /// </summary>
        [Bindable(true)]
        public object HeaderContent
        {
            get { return (object)GetValue(HeaderContentProperty); }
            set { SetValue(HeaderContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderContentProperty =
            DependencyProperty.Register("HeaderContent", typeof(object), typeof(AiWindow));


        /// <summary>
        /// 頭部標(biāo)題欄文字顏色
        /// </summary>
        [Bindable(true)]
        public Brush HeaderForeground
        {
            get { return (Brush)GetValue(HeaderForegroundProperty); }
            set { SetValue(HeaderForegroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderForeground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderForegroundProperty =
            DependencyProperty.Register("HeaderForeground", typeof(Brush), typeof(AiWindow), new PropertyMetadata(Brushes.White));


        /// <summary>
        /// 頭部標(biāo)題欄背景色
        /// </summary>
        [Bindable(true)]
        public Brush HeaderBackground
        {
            get { return (Brush)GetValue(HeaderBackgroundProperty); }
            set { SetValue(HeaderBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderBackgroundProperty =
            DependencyProperty.Register("HeaderBackground", typeof(Brush), typeof(AiWindow));
        /// <summary>
        /// 是否顯示頭部標(biāo)題欄
        /// </summary>
        [Bindable(true)]
        public bool IsShowHeader
        {
            get { return (bool)GetValue(IsShowHeaderProperty); }
            set { SetValue(IsShowHeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsShowHeader.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsShowHeaderProperty =
            DependencyProperty.Register("IsShowHeader", typeof(bool), typeof(AiWindow), new PropertyMetadata(false));

        /// <summary>
        /// 動(dòng)畫類型
        /// </summary>
        [Bindable(true)]
        public AnimationType Type
        {
            get { return (AnimationType)GetValue(TypeProperty); }
            set { SetValue(TypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Type.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TypeProperty =
            DependencyProperty.Register("Type", typeof(AnimationType), typeof(AiWindow), new PropertyMetadata(AnimationType.Default));



        /// <summary>
        /// 頭部高度
        /// </summary>
        public int HeaderHeight
        {
            get { return (int)GetValue(HeaderHeightProperty); }
            set { SetValue(HeaderHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderHeightProperty =
            DependencyProperty.Register("HeaderHeight", typeof(int), typeof(AiWindow), new PropertyMetadata(50));






        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            PART_CloseWindowButton = GetTemplateChild("PART_CloseWindowButton") as Button;
            PART_MaxWindowButton = GetTemplateChild("PART_MaxWindowButton") as Button;
            PART_MinWindowButton = GetTemplateChild("PART_MinWindowButton") as Button;
            if (PART_MaxWindowButton != null && PART_MinWindowButton != null && PART_CloseWindowButton != null)
            {
                PART_MaxWindowButton.Click -= PART_MaxWindowButton_Click;
                PART_MinWindowButton.Click -= PART_MinWindowButton_Click;
                PART_CloseWindowButton.Click -= PART_CloseWindowButton_Click;
                PART_MaxWindowButton.Click += PART_MaxWindowButton_Click;
                PART_MinWindowButton.Click += PART_MinWindowButton_Click;
                PART_CloseWindowButton.Click += PART_CloseWindowButton_Click;
            }
        }

        private void PART_CloseWindowButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void PART_MinWindowButton_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void PART_MaxWindowButton_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }
            else
            {
                WindowState = WindowState.Normal;
            }
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            if (SizeToContent == SizeToContent.WidthAndHeight && WindowChrome.GetWindowChrome(this) != null)
            {
                InvalidateMeasure();
            }
        }
    }
}

注意:在自定義控件中,設(shè)置窗口標(biāo)題的屬性要在樣式中進(jìn)行綁定,所以需要定義為依賴屬性。在此控件中用到的通用的類,則存放在Utils中。

?

3. 創(chuàng)建自定義控件樣式

?

在Styles文件夾中,創(chuàng)建樣式資源文件【AiWindowStyle.xaml】,并將樣式的TargentType指定為Cotrols中創(chuàng)建的自定義類AiWindow。然后修改控件的Template屬性,為其定義新的的ControlTemplate,來改變控件的樣式。如下所示:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Ai="clr-namespace:SmallSixUI.Templates.Controls">
    <WindowChrome
        x:Key="LayWindowChromeStyle"
        CaptionHeight="56"
        CornerRadius="0"
        GlassFrameThickness="1"
        NonClientFrameEdges="None"
        ResizeBorderThickness="4"
        UseAeroCaptionButtons="False" />
    <Style x:Key="AiWindowStyle" TargetType="Ai:AiWindow">
        <Setter Property="Background" Value="White" />
        <Setter Property="HeaderBackground" Value="#23262E" />
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome
                    CaptionHeight="50"
                    CornerRadius="0"
                    GlassFrameThickness="1"
                    NonClientFrameEdges="None"
                    ResizeBorderThickness="4"
                    UseAeroCaptionButtons="False" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Ai:AiWindow">
                    <Border
                        Height="{TemplateBinding HeaderHeight}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        ClipToBounds="True">
                        <Border.Style>
                            <Style TargetType="Border">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Ai:AiWindow}, Path=WindowState}" Value="Maximized">
                                        <Setter Property="Padding" Value="7" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Border.Style>
                        <Ai:AiTransition Style="{DynamicResource LayTransitionStyle}" Type="{TemplateBinding Type}">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto" />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid
                                    x:Name="PART_Header"
                                    Background="{TemplateBinding HeaderBackground}">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto" />
                                        <ColumnDefinition Width="auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="auto" />
                                    </Grid.ColumnDefinitions>
                                    <Image
                                        x:Name="Icon"
                                        Width="32"
                                        Height="32"
                                        Margin="5"
                                        Source="{TemplateBinding Icon}"
                                        Stretch="Fill" />
                                    <TextBlock
                                        x:Name="HeaderText"
                                        Grid.Column="1"
                                        Margin="5,0"
                                        FontSize="13"
                                        VerticalAlignment="Center"
                                        Foreground="{TemplateBinding HeaderForeground}"
                                        Text="{TemplateBinding Title}" />
                                    <ContentPresenter
                                        Grid.Column="2"
                                        ContentSource="HeaderContent"
                                        WindowChrome.IsHitTestVisibleInChrome="true" />
                                    <StackPanel Grid.Column="3" Orientation="Horizontal">
                                        <StackPanel.Resources>
                                            <ControlTemplate x:Key="WindowButtonTemplate" TargetType="Button">
                                                <Grid x:Name="body" Background="Transparent">
                                                    <Border
                                                        Margin="3"
                                                        Background="Transparent"
                                                        WindowChrome.IsHitTestVisibleInChrome="True" />
                                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                </Grid>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="IsMouseOver" Value="True">
                                                        <Setter TargetName="body" Property="Background">
                                                            <Setter.Value>
                                                                <SolidColorBrush Opacity="0.1" Color="Black" />
                                                            </Setter.Value>
                                                        </Setter>
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </StackPanel.Resources>
                                        <Button
                                            x:Name="PART_MinWindowButton"
                                            Width="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualHeight}"
                                            Cursor="Hand"
                                            Template="{DynamicResource WindowButtonTemplate}">
                                            <Path
                                                Width="15"
                                                Height="2"
                                                Data="M772.963422 533.491105l-528.06716 0c-12.38297 0-22.514491-10.131521-22.514491-22.514491l0 0c0-12.38297 10.131521-22.514491 22.514491-22.514491l528.06716 0c12.38297 0 22.514491 10.131521 22.514491 22.514491l0 0C795.477913 523.359584 785.346392 533.491105 772.963422 533.491105z"
                                                Fill="{TemplateBinding HeaderForeground}"
                                                IsHitTestVisible="false"
                                                Stretch="Fill" />
                                        </Button>
                                        <Button
                                            x:Name="PART_MaxWindowButton"
                                            Width="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualHeight}"
                                            Cursor="Hand"
                                            Template="{DynamicResource WindowButtonTemplate}">
                                            <Path
                                                x:Name="winCnangePath"
                                                Width="15"
                                                Height="15"
                                                Data="M10.62,72a9.92,9.92,0,0,1-4-.86A11.15,11.15,0,0,1,.86,65.43,9.92,9.92,0,0,1,0,61.38V25A9.86,9.86,0,0,1,.86,21,11.32,11.32,0,0,1,3.18,17.6a11,11,0,0,1,3.38-2.32,9.68,9.68,0,0,1,4.06-.87H47a9.84,9.84,0,0,1,4.08.87A11,11,0,0,1,56.72,21,9.84,9.84,0,0,1,57.59,25V61.38a9.68,9.68,0,0,1-.87,4.06,11,11,0,0,1-2.32,3.38A11.32,11.32,0,0,1,51,71.14,9.86,9.86,0,0,1,47,72Zm36.17-7.21a3.39,3.39,0,0,0,1.39-.28,3.79,3.79,0,0,0,1.16-.77,3.47,3.47,0,0,0,1.07-2.53v-36a3.55,3.55,0,0,0-.28-1.41,3.51,3.51,0,0,0-.77-1.16,3.67,3.67,0,0,0-1.16-.77,3.55,3.55,0,0,0-1.41-.28h-36a3.45,3.45,0,0,0-1.39.28,3.59,3.59,0,0,0-1.14.79,3.79,3.79,0,0,0-.77,1.16,3.39,3.39,0,0,0-.28,1.39v36a3.45,3.45,0,0,0,.28,1.39A3.62,3.62,0,0,0,9.4,64.51a3.45,3.45,0,0,0,1.39.28Zm18-43.45a13.14,13.14,0,0,0-1.16-5.5,14.41,14.41,0,0,0-3.14-4.5,15,15,0,0,0-4.61-3,14.14,14.14,0,0,0-5.5-1.1H15A10.73,10.73,0,0,1,21.88.51,10.93,10.93,0,0,1,25.21,0H50.38a20.82,20.82,0,0,1,8.4,1.71A21.72,21.72,0,0,1,70.29,13.18,20.91,20.91,0,0,1,72,21.59v25.2a10.93,10.93,0,0,1-.51,3.33A10.71,10.71,0,0,1,70,53.05a10.84,10.84,0,0,1-2.28,2.36,10.66,10.66,0,0,1-3,1.58Z"
                                                Fill="{TemplateBinding HeaderForeground}"
                                                IsHitTestVisible="false"
                                                Stretch="Fill" />
                                        </Button>
                                        <Button
                                            x:Name="PART_CloseWindowButton"
                                            Width="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualHeight}"
                                            Cursor="Hand"
                                            Template="{DynamicResource WindowButtonTemplate}">
                                            <Path
                                                Width="15"
                                                Height="15"
                                                Data="M550.848 502.496l308.64-308.896a31.968 31.968 0 1 0-45.248-45.248l-308.608 308.896-308.64-308.928a31.968 31.968 0 1 0-45.248 45.248l308.64 308.896-308.64 308.896a31.968 31.968 0 1 0 45.248 45.248l308.64-308.896 308.608 308.896a31.968 31.968 0 1 0 45.248-45.248l-308.64-308.864z"
                                                Fill="{TemplateBinding HeaderForeground}"
                                                IsHitTestVisible="false"
                                                Stretch="Fill" />
                                        </Button>
                                    </StackPanel>
                                </Grid>
                                <Grid Grid.Row="1" ClipToBounds="True">
                                    <ContentPresenter />
                                </Grid>
                            </Grid>
                        </Ai:AiTransition>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="WindowState" Value="Normal">
                            <Setter TargetName="winCnangePath" Property="Data" Value="M10.62,72a9.92,9.92,0,0,1-4-.86A11.15,11.15,0,0,1,.86,65.43,9.92,9.92,0,0,1,0,61.38V10.62a9.92,9.92,0,0,1,.86-4A11.15,11.15,0,0,1,6.57.86a9.92,9.92,0,0,1,4-.86H61.38a9.92,9.92,0,0,1,4.05.86,11.15,11.15,0,0,1,5.71,5.71,9.92,9.92,0,0,1,.86,4V61.38a9.92,9.92,0,0,1-.86,4.05,11.15,11.15,0,0,1-5.71,5.71,9.92,9.92,0,0,1-4.05.86Zm50.59-7.21a3.45,3.45,0,0,0,1.39-.28,3.62,3.62,0,0,0,1.91-1.91,3.45,3.45,0,0,0,.28-1.39V10.79a3.45,3.45,0,0,0-.28-1.39A3.62,3.62,0,0,0,62.6,7.49a3.45,3.45,0,0,0-1.39-.28H10.79a3.45,3.45,0,0,0-1.39.28A3.62,3.62,0,0,0,7.49,9.4a3.45,3.45,0,0,0-.28,1.39V61.21a3.45,3.45,0,0,0,.28,1.39A3.62,3.62,0,0,0,9.4,64.51a3.45,3.45,0,0,0,1.39.28Z" />
                        </Trigger>
                        <Trigger Property="IsShowHeader" Value="false">
                            <Setter TargetName="PART_Header" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="ResizeMode" Value="NoResize">
                            <Setter TargetName="PART_MaxWindowButton" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="Icon" Value="{x:Null}">
                            <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger SourceName="HeaderText" Property="Text" Value="">
                            <Setter TargetName="HeaderText" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger SourceName="HeaderText" Property="Text" Value="{x:Null}">
                            <Setter TargetName="HeaderText" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

?

4. 創(chuàng)建默認(rèn)主題

?

在Themes文件夾中,創(chuàng)建主題資源文件【Generic.xaml】,并在主題資源文件中,引用上述創(chuàng)建的樣式資源,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/SmallSixUI.Templates;component/Styles/AiWindowStyle.xaml"></ResourceDictionary>
        <ResourceDictionary Source="pack://application:,,,/SmallSixUI.Templates;component/Styles/AiTransitionStyle.xaml"></ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

主題:主題資源文件,是為了整合一組資源樣式,來形成一套主題。同一主題之內(nèi),風(fēng)格統(tǒng)一;不同主題之間相互獨(dú)立,風(fēng)格迥異

?

應(yīng)用自定義控件庫

?

1. 創(chuàng)建應(yīng)用程序

?

在解決方案中,創(chuàng)建WPF應(yīng)用程序【SmallSixUI.App】,并引用控件庫【SmallSixUI.Templates】,如下所示:

WPF自定義控件庫之Window窗口

?

2. 引入主題資源

?

在應(yīng)用程序的啟動(dòng)類App.xaml中,引入主題資源文件,如下所示:

<Application x:Class="SmallSixUI.App.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SmallSixUI.App"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/SmallSixUI.Templates;component/Themes/Generic.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

?

3. 創(chuàng)建測試窗口

?

在應(yīng)用程序中,創(chuàng)建測試窗口【SmallSixWindow.xaml】,添加控件庫命名控件【Ai】,并修改窗口的繼承類為【AiWindow】,然后設(shè)置窗口的背景色,logo,標(biāo)題等,如下所示:

SmallSixWindow.xaml文件中,如下所示:

<Ai:AiWindow x:Class="SmallSixUI.App.SmallSixWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Ai="clr-namespace:SmallSixUI.Templates.Controls;assembly=SmallSixUI.Templates"
        xmlns:local="clr-namespace:SmallSixUI.App"
        mc:Ignorable="d"
        IsShowHeader="True"
        HeaderBackground="#446180"
        HeaderHeight="80"
        Icon="logo.png" 
        Title="小六公子的UI設(shè)計(jì)窗口" Height="450" Width="800">
    <Grid>
        
    </Grid>
</Ai:AiWindow>

?SmallSixWindow.xaml.cs中修改繼承類,如下所示:

using SmallSixUI.Templates.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SmallSixUI.App
{
    /// <summary>
    /// XWindow.xaml 的交互邏輯
    /// </summary>
    public partial class SmallSixWindow : AiWindow
    {
        public SmallSixWindow()
        {
            InitializeComponent();
        }
    }
}

?

運(yùn)行程序

?

通過Visual Studio運(yùn)行程序,如下所示:

普通默認(rèn)窗口,如下所示:

WPF自定義控件庫之Window窗口

自定義窗口

應(yīng)用自定義樣式的窗口,如下所示:

WPF自定義控件庫之Window窗口

以上就是WPF自定義控件庫之窗口的全部內(nèi)容。希望可以拋磚引玉,一起學(xué)習(xí),共同進(jìn)步。文章來源地址http://www.zghlxwxcb.cn/news/detail-719635.html

到了這里,關(guān)于WPF自定義控件庫之Window窗口的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 循序漸進(jìn)介紹基于CommunityToolkit.Mvvm 和HandyControl的WPF應(yīng)用端開發(fā)(3)--自定義用戶控件

    循序漸進(jìn)介紹基于CommunityToolkit.Mvvm 和HandyControl的WPF應(yīng)用端開發(fā)(3)--自定義用戶控件

    在我們創(chuàng)建界面元素的時(shí)候,不管在Vue3+ElementPlus的前端上,還是Winform桌面端上,都是會(huì)利用自定義用戶控件來快速重用一些自定義的界面內(nèi)容,對(duì)自定義用戶控件的封裝處理,也是我們開發(fā)WPF應(yīng)用需要熟悉的一環(huán)。本篇隨筆繼續(xù)深入介紹介紹基于CommunityToolkit.Mvvm 和HandyCont

    2024年02月09日
    瀏覽(20)
  • 如何使用 WPF 用戶控件關(guān)閉父窗口

    How to close parent windows using WPF User Control 如何使用 WPF 用戶控件關(guān)閉父窗口 【問題描述】 假設(shè)有兩個(gè)WPF窗口:window1和window2。 window1有一個(gè)按鈕,單擊此按鈕將打開window2。window2包含一個(gè)用戶控件。此用戶控件有一個(gè)用于關(guān)閉window2的按鈕。 怎樣才能實(shí)現(xiàn)這個(gè)場景呢? 【解決方案

    2024年02月15日
    瀏覽(38)
  • WPF常用控件-選擇文件與保存文件窗口

    OpenFileDialog 類型位于 Microsoft.Win32 命名空間下,用于在WPF中彈出文件選擇窗口進(jìn)行文件的選擇。 常用屬性 Filter :過濾器,可以設(shè)置可以選擇的文件類型。 InitialDirectory :文件選擇框的初始目錄。 Multiselect :是否支持多選。 FileName `FileNames`:所選文件的文件名。 常用方法 b

    2024年01月23日
    瀏覽(25)
  • C# wpf 實(shí)現(xiàn)任意控件(包括窗口)更多調(diào)整大小功能

    C# wpf 實(shí)現(xiàn)任意控件(包括窗口)更多調(diào)整大小功能

    第一節(jié) Grid內(nèi)控件拖動(dòng)調(diào)整大小 第二節(jié) Canvas內(nèi)控件拖動(dòng)調(diào)整大小 第三節(jié) 窗口拖動(dòng)調(diào)整大小 第四節(jié) 附加屬性實(shí)現(xiàn)拖動(dòng)調(diào)整大小 第五章 拓展更多調(diào)整大小功能(本章) 上一章我們已經(jīng)實(shí)現(xiàn)了任意控件統(tǒng)一的拖動(dòng)調(diào)整功能,能夠方便的給任意控件設(shè)置拖動(dòng)調(diào)整大小。開發(fā)過程

    2024年01月18日
    瀏覽(22)
  • 循序漸進(jìn)介紹基于CommunityToolkit.Mvvm 和HandyControl的WPF應(yīng)用端開發(fā)(11) -- 下拉列表的數(shù)據(jù)綁定以及自定義系統(tǒng)字典列表控件

    循序漸進(jìn)介紹基于CommunityToolkit.Mvvm 和HandyControl的WPF應(yīng)用端開發(fā)(11) -- 下拉列表的數(shù)據(jù)綁定以及自定義系統(tǒng)字典列表控件

    在我們開發(fā)的前端項(xiàng)目中,往往為了方便,都需對(duì)一些控件進(jìn)行自定義的處理,以便實(shí)現(xiàn)快速的數(shù)據(jù)綁定以及便捷的使用,本篇隨筆介紹通過抽取常見字典列表,實(shí)現(xiàn)通用的字典類型綁定;以及通過自定義控件的屬性處理,實(shí)現(xiàn)系統(tǒng)字典內(nèi)容的快捷綁定的操作。 在我們創(chuàng)建下

    2024年02月08日
    瀏覽(19)
  • 合宙Air724UG LuatOS-Air LVGL API控件-窗口 (Window)

    合宙Air724UG LuatOS-Air LVGL API控件-窗口 (Window)

    窗口 (Window) 分 享導(dǎo)出pdf | lvgl.win_create 調(diào)用 lvgl.win_create(par, copy) 功能 創(chuàng)建一個(gè)窗口對(duì)象 返回 指向創(chuàng)建的窗口的指針 參數(shù) par 指向?qū)ο蟮闹羔? 它將是新鍵對(duì)象的父對(duì)象 copy 指向窗口對(duì)象的指針, 如果不為 nil, 則將從其復(fù)制新對(duì)象 lvgl.win_clean 調(diào)用 lvgl.win_clean(win) 功能 清除窗口

    2024年02月09日
    瀏覽(26)
  • WPF中用戶控件和自定義控件

    WPF中用戶控件和自定義控件

    無論是在WPF中還是WinForm中,都有用戶控件(UserControl)和自定義控件(CustomControl),這兩種控件都是對(duì)已有控件的封裝,實(shí)現(xiàn)功能重用。但是兩者還是有一些區(qū)別,本文對(duì)這兩種控件進(jìn)行講解。 用戶控件 注重復(fù)合控件的使用,也就是多個(gè)現(xiàn)有控件組成一個(gè)可復(fù)用的控件組

    2024年01月21日
    瀏覽(26)
  • WPF自定義按鈕控件

    WPF自定義按鈕控件

    在平時(shí)的WPF應(yīng)用中,系統(tǒng)提供的按鈕控件確實(shí)可以實(shí)現(xiàn)正常的邏輯,但是從視覺方面看的話,確實(shí)不夠美觀,而且每個(gè)項(xiàng)目的UI設(shè)計(jì)不盡相同。那么自定義按鈕控件就是必須的了,網(wǎng)上查找了很多自定義按鈕控件的辦法,但每次都是寫到一半就報(bào)錯(cuò)。在參考了多個(gè)技術(shù)貼之后

    2024年02月08日
    瀏覽(22)
  • WPF自定義控件

    WPF自定義控件

    方式一:基于現(xiàn)有控件進(jìn)行擴(kuò)展,如基于button進(jìn)行擴(kuò)展,UI可直接用xmal進(jìn)行編輯設(shè)計(jì),邏輯用xaml.cs進(jìn)行編輯 方法二:直接創(chuàng)建wpf自定義控件 本文用方法二開展自定義控件?。?! 1.自定義控件的內(nèi)容在代碼cs文件中,自定義控件繼承自Control,ui界面可在Genric.xaml中定義。 2.在

    2024年02月11日
    瀏覽(30)
  • wpf 自定義combox控件

    wpf 自定義combox控件

    關(guān)鍵步驟 1、新建usercontrol使用基本的控件進(jìn)行設(shè)計(jì) 2、依賴屬性的定義,目的:外部調(diào)用時(shí)候能夠使用屬性進(jìn)行控件樣式的控制 例如 Width=\\\"200\\\" DisplayMemberPath=\\\"Name\\\" SelectedItem=\\\"{Binding SelectedItem,Mode=TwoWay}\\\" SelectionChanged=\\\"{Binding ProjectSelectCommand}\\\" CommandParameter=\\\"{Binding ElementName = Projec

    2024年02月09日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包