主頁 > .NET開發 > WPF教程十三:自定義控制元件進階可視化狀態與自定義Panel

WPF教程十三:自定義控制元件進階可視化狀態與自定義Panel

2021-06-09 14:59:57 .NET開發

如果你敲了上一篇的代碼,經過上一篇各種問題的蹂躪,我相信自定義控制元件基礎部分其實已經了解的七七八八了,那么我們開始進階,現在這篇講的才是真正會用到的核心的東西,簡化你的代碼,給你提供更多的可能,掌握了這篇,才能發揮出來WPF的威力,這一篇學完我們的鳥槍就要換大炮了,

ColorPicker例子分離了行為和可視化外觀,其他人員可以動態改變外觀的模板,因為不涉及到狀態,所以來說相對簡單,現在我們來基于現在的內容深入一個難的,

首先創建通過自定義FlipContentPanel控制元件學習自定義控制元件下VisualStateManager的使用,一個同型別的資料,我們給2個展示狀態,一個是簡易的頁面、另外一個是詳細的頁面,我們通過一個狀態切換這2種不同UI的呈現效果,狀態切換時播放一個過渡影片,這一篇主要是基于上一篇的ColorPicker學習到的自定義控制元件的知識結合visualStateManager、影片來實作一個基于狀態切換的頁面,通過狀態來管理并控制頁面呈現的內容,這樣就可以通過狀態來實作不同的外觀,同時基于自定義控制元件可以很輕松的實作復雜的效果,并且代碼易于維護,

首先,我們創建一個繼承自Control的FlipContentPanel自定義無外觀控制元件類,該類包含2個狀態:Flipped和Normal,

我們將使用是否是Flipped來控制頁面切換2個不同的呈現內容,

重復使用propdp=>2次tab創建我們需要的3個依賴項屬性DetailsContent、OverviewContent、IsFlipped,代碼如下:

 public object DetailsContent
        {
            get { return (object)GetValue(DetailsContentProperty); }
            set { SetValue(DetailsContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DetailsContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DetailsContentProperty =
            DependencyProperty.Register("DetailsContent", typeof(object), typeof(FlipContentPanel));
        public object OverviewContent
        {
            get { return (object)GetValue(OverviewContentProperty); }
            set { SetValue(OverviewContentProperty, value); }
        }

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

        public bool IsFlipped
        {
            get { return (bool)GetValue(IsFlippedProperty); }
            set
            {
                SetValue(IsFlippedProperty, value);
            }
        }

  // Using a DependencyProperty as the backing store for IsFlipped.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsFlippedProperty =
            DependencyProperty.Register("IsFlipped", typeof(bool), typeof(FlipContentPanel));

在靜態建構式中給FlipContentPanel.cs添加默認外觀,

 static FlipContentPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FlipContentPanel), new FrameworkPropertyMetadata(typeof(FlipContentPanel)));
        }

我們設計了2個頁面,一個DetailsContent頁面,一個OverviewContent頁面,我們在這2個頁面的默認樣式中各添加一個按鈕,用來控制切換VisualState,在上一篇講的OnApplyTemplate()中我們從模板中獲取控制元件,然后系結觸發事件,這一篇我們結合模板中的按鈕在onApplyTemplate()的程序中查找元素并添加事件,用于控制切換狀態,這樣內置集成在自定義控制元件中的好處是如果有元素了就附加事件,如果沒有就不附加事件,兩個按鈕起名為FlipButton和FlipButtonAlternate,

 public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ToggleButton flipButton = base.GetTemplateChild("FlipButton") as ToggleButton;
            if (flipButton != null)
            {
                flipButton.Click += FlipButton_Click;
            }

            ToggleButton flipAlternateButton = base.GetTemplateChild("FlipButtonAlternate") as ToggleButton;
            if (flipAlternateButton != null)
            {
                flipAlternateButton.Click += FlipButton_Click;
            }
            ChangedVisualState(false);
        }

        private void FlipButton_Click(object sender, RoutedEventArgs e)
        {
            this.IsFlipped = !this.IsFlipped 
        }

		protected void ChangedVisualState(bool value)
        {
            if (IsFlipped)
            {
                VisualStateManager.GoToState(this, "Flipped", value);
            }
            else
            {
                VisualStateManager.GoToState(this, "Normal", value);
            }
        }

同時修改依賴項屬性IsFlipped的Set方法,當IsFlipped發生改變時,去修改VisualState,(PS:這里經過大佬們的提醒,在這種屬性值的Get、Set里盡量不要寫內容,容易養成習慣以后給自己留坑,SetValue代碼走不到,這部分代碼修改如下:)

   public bool IsFlipped
        {
            get { return (bool)GetValue(IsFlippedProperty); }
            set
            {
                SetValue(IsFlippedProperty, value); 
            }
        }
        // Using a DependencyProperty as the backing store for IsFlipped.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsFlippedProperty =
            DependencyProperty.Register("IsFlipped", typeof(bool), typeof(FlipContentPanel),new PropertyMetadata(false,IsFlippedChanged));

        private static void IsFlippedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { 
            if (d is FlipContentPanel flipContentPanel)
            {
                flipContentPanel.ChangedVisualState(true);
            }  
        } 

這樣加上2個狀態,2個按鈕在模板中我們就有4個物件,我們使用特性在類上方標明這4個物件,

 	[TemplateVisualState(Name = "Flipped",GroupName = "VisualState")]
    [TemplateVisualState(Name ="Normal",GroupName = "VisualState")]
    [TemplatePart(Name = "FlipButton", Type = typeof(ToggleButton))]
    [TemplatePart(Name = "FlipButtonAlternate", Type = typeof(ToggleButton))]
    public class FlipContentPanel : Control

這樣我們的FlipContentPanel無外觀自定義控制元件就寫完了,他包含了2個狀態,會從控制元件模板中獲取2個按鈕,并添加事件,用于切換頁面,在靜態建構式中還會讀取默認外觀,

接下來我們去寫FlipContentPanel的默認外觀,

新建一個資源字典名字叫做FlipContentPanel:

在其中添加Style,TargetType為我們的FlipContentPanel并重寫template,

Template中的ContrlTemplate我們在一個Grid中寫2個同樣大小和位置的Border,這2個border中包含我們用來呈現2個不同頁面內容的ContentPresenter,然后通過切換2個Border的Visibility屬性來控制2個border的(相當于頁面的)顯示或隱藏的切換,

對應的ContentPresenter 系結我們的依賴項屬性OverviewContent和DetailsContent,每個ContentPresenter上面還有一個固定位置的ToggleButton,用來切換當前的顯示內容,這2個ToggleButton的Name保持和無外觀控制元件中我們定義的FlipButton和FlipButtonAlternate一致,用于在OnApplyTemplate()階段能給這2個ToggleButton系結事件,用于切換VisualState,同時我們在ControlTemplate中添加VisualState的管理器,用于切換狀態,其實VisualStateManager這里想使用的好也需要單獨講一下,我在這里就被坑了2天,想實作的功能特別牛逼,但是發現這里如果應用不好的話,問題會特別多,實作出來的效果跟預期不一樣,以后會單獨在Blend下講這個VisualStateManager,因為他有VisualStateGrounps的概念,可以很好的完成很多種狀態之前的切換,這里我們就寫一個最簡單的切換程序中漸顯和漸隱,整體代碼如下:

 <Style TargetType="{x:Type local:FlipContentPanel}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:FlipContentPanel}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="VisualState">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="Normal" To="Flipped" GeneratedDuration="0:0:0.5">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DetailsContentBorder">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="https://www.cnblogs.com/duwenlong/p/0"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="https://www.cnblogs.com/duwenlong/p/1"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition From="Flipped" To="Normal" GeneratedDuration="0:0:0.5" >
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DetailsContentBorder">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="https://www.cnblogs.com/duwenlong/p/1"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:0.5"  Value="https://www.cnblogs.com/duwenlong/p/0"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Flipped">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="OverviewContentBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="https://www.cnblogs.com/duwenlong/p/{x:Static Visibility.Hidden}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Normal">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="DetailsContentBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="https://www.cnblogs.com/duwenlong/p/{x:Static Visibility.Hidden}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups> 
                        <Border x:Name="OverviewContentBorder" 
                                Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:FlipContentPanel},Mode=FindAncestor},Path=ActualHeight}" 
                                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:FlipContentPanel},Mode=FindAncestor},Path=ActualWidth}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}">
                            <Grid>
                                <ContentPresenter Content="{TemplateBinding OverviewContent}"/>
                                <ToggleButton x:Name="FlipButton" Width="50" Height="50" VerticalAlignment="Bottom" HorizontalAlignment="Left"  Content="顯示詳情"/>
                            </Grid>
                        </Border>
                        <Border x:Name="DetailsContentBorder" BorderBrush="{TemplateBinding BorderBrush}" VerticalAlignment="Bottom" HorizontalAlignment="Left" 
                                Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:FlipContentPanel},Mode=FindAncestor},Path=ActualHeight}" 
                                Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:FlipContentPanel},Mode=FindAncestor},Path=ActualWidth}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}">
                            <Grid>
                                <ContentPresenter Content="{TemplateBinding DetailsContent}"/>
                                <ToggleButton x:Name="FlipButtonAlternate" Width="50" Height="50" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="30" Content="收起"/>
                            </Grid>
                        </Border> 
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

注意上面的代碼中From="Normal" To="Flipped" 和From="Flipped" To="Normal"2個VisualTransition,它們是狀態切換時執行的,是狀態切換后執行的,所以我設定了不同的影片,這里一定要多練以下,這里整體就這么多,配個圖把,這里卡了我3個晚上,

XAML使用的代碼如下,然后配個圖:

<Window x:
        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:local="clr-namespace:CustomElement"  
        xmlns:usercontrols="clr-namespace:CustomElement.UserControls"
        xmlns:lib="clr-namespace:CustomControls;assembly=CustomControls"  
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
             <Grid Background="Firebrick">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <lib:FlipContentPanel x:Name="flipPanel" Background="AntiqueWhite" Grid.Row="1" BorderBrush="DarkBlue" BorderThickness="3" IsFlipped="false">
            <lib:FlipContentPanel.DetailsContent>
                <Grid>
                    <TextBlock Background="Red" Text="我是詳情頁"/>
                </Grid>
            </lib:FlipContentPanel.DetailsContent>
            <lib:FlipContentPanel.OverviewContent>
                <Grid>
                    <TextBlock Background="Yellow" Text="我是串列頁"/>
                </Grid>
            </lib:FlipContentPanel.OverviewContent>
        </lib:FlipContentPanel> 
    </Grid> 
</Window>

進來下更進一步積累自定義控制元件的知識,學習自定義面板及構建自定義繪圖控制元件,

創建自定義面板是一種比較常見的自定義控制元件開發子集,面板駐留一個或多個子元素,并且實作了特定的布局邏輯以恰當地安排其子元素,如果希望構建自己的可拖動的工具列或可停靠的視窗系統,自定義面板是很重要的元素,當創建需要非標準特定布局的組合控制元件時,自定義面板通常是很有用的,例如停靠工具列,

面板在作業時,主要有2件事情:負責改變子元素尺寸和安排子元素的兩步布局程序,第一個階段是測量階段(measure pass),在這一階段面板決定其子元素希望具有多大的尺寸,第二個階段是排列階段(layout pass),在這一階段為每個控制元件指定邊界,這兩個步驟是必須的,因為在決定如何分割可用空間時,面板需要考慮所有子元素的期望,

可以通過重寫MeasureOverride()和ArrangeOverride()方法,為這兩個步驟添加自己的邏輯,這兩個方法作為WPF布局系統的一部分在FrameworkElement類種定義的,使用MeasureOverride()和ArrangeOverride()方法代替在UIElement類中定義的MeasureCore()和ArrangeCore(),這2個方法是不能被重寫的,

接下來我們分析一下這2個方法都在做什么:

1.MeasureOverride()

首先使用MeasureOverride()方法決定每個子元素希望多大的空間,每個MeasureOverride()方法的實作負責遍歷子元素集合,并呼叫每個子元素的Measure()方法,當呼叫Measure()方法時,需要提供邊界框-決定每個子控制元件最大可用控制元件的Size物件,在MeasureOverride()方法的最后,面板回傳顯示所有子元素所需的空間,并回傳它們所期望的尺寸,在測量程序的結尾,布局容器必須回傳它所期望的尺寸,在簡單的面板中,可以通過組合每個資源需要的期望尺寸計算面板所期望的尺寸,

2.ArrangeOverride()方法

測量完所有元素后,就可以在可用的空間中排列元素了,布局系統呼叫面板的ArrangeOverride()方法,而面板為每個子元素呼叫Arrange()方法,以告訴子元素為它分配了多大的空間,

當時有Measure()方法測量條目時,傳遞能夠定義可用空間邊界的Size物件,當時有Arrange()方法放置條目時,傳遞能夠定義條目尺寸和位置的System.Windows.Rect物件,

了解了這兩步,我們來實作一個Canvas面板,

Canvas面板在它們希望的位置放置子元素,并且為子元素設定它們希望的尺寸,所以,Canvas面板不需要計算如何分割可用空間,MeasureOverride()階段可以為每個子元素提供無限的空間,

  protected override Size MeasureOverride(Size availableSize)
        {
            Size size = new Size(double.PositiveInfinity, double.PositiveInfinity);
            foreach (UIElement element in base.InternalChildren)
            {
                element.Measure(size);
            }
            return new Size();
        }

在MeasureOverride()方法回傳空的Size物件,也就是說Canvas面板不請求任何空間,而是我們明確的為Canvas面板指定尺寸,或者將其放置到布局容器中進行拉伸以填充整個容器的可用空間,

ArrangeOverride()方法包含的內容稍微多一些,為了確定每個元素的正確位置,Canvas面板使用附加屬性Left、Right、Top以及Bottom,我們只用Left和Top附加依賴項屬性來實作一個簡易版,

public class CanvasClone : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            Size size = new Size(double.PositiveInfinity, double.PositiveInfinity);
            foreach (UIElement element in base.InternalChildren)
            {
                element.Measure(size);
            }
            return new Size();
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            foreach (UIElement element in base.InternalChildren)
            {
                double x = 0;
                double y = 0;
                double left = Canvas.GetLeft(element);
                if (!double.IsNaN(left))
                {
                    x = left;
                }
                double top = Canvas.GetTop(element);
                if(!double.IsNaN(top))
                {
                    y = top;
                }
                element.Arrange(new Rect(new Point(x, y), element.DesiredSize)); 
            }
            return finalSize;
        }
    }

Xaml代碼:

<Window x:
        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:local="clr-namespace:CustomElement"
        xmlns:customPanel="clr-namespace:CustomElement.Panels"
        mc:Ignorable="d"
        Title="CustomPanel" Height="450" Width="800">
    <Grid>
        <customPanel:CanvasClone>
            <TextBlock Text="asg" Canvas.Left="100" Canvas.Top="100"/>
        </customPanel:CanvasClone> 
    </Grid>
</Window>

我們就看到了TextBlock被放置在了靠近左上角100,100的位置,,這里不考慮其他問題,因為只是為了了解自定義面板,

接下來我們創建一個擴展的WrapPanel面板,WrapPanel的作業原理是,該面板逐個布置其子元素,一旦當前行寬度用完,就會切換到下一行,但有時我們需要強制立即換行,以便在新行中啟動某個特定控制元件,盡管WrapPanel面板原本沒有提供這一功能,但通過創建自定義控制元件可以方便地添加該功能,只需要添加一個請求換行的附加依賴項屬性即可,此后,面板中的子元素可使用該屬性在適當位置換行,

我們添加WrapBreakPanel類,繼承自Panel,這里因為要自定義所以不使用代碼片段添加附加依賴項屬性,而是手寫,并設定AffectsMeasure和AffectsArrange為True,我們要在每次LineBreakBefore屬性變更時,都觸發新的排列階段,在測量階段元素按行排列,除非太大或者LineBreakBefore屬性設定為true,否則每個元素都被添加到當前行中,

using System;
using System.Windows;
using System.Windows.Controls;

namespace CustomElement.Panels
{
    public class WrapBreakPanel : Panel
    {

        static WrapBreakPanel()
        {
            FrameworkPropertyMetadata metadata = https://www.cnblogs.com/duwenlong/p/new FrameworkPropertyMetadata();
            metadata.AffectsArrange = true;
            metadata.AffectsMeasure = true;
            LineBreakBeforeProperty =
             DependencyProperty.RegisterAttached("LineBreakBefore", typeof(bool), typeof(WrapBreakPanel), metadata);
        }

        public static readonly DependencyProperty LineBreakBeforeProperty;

        public static void SetLineBreakBefore(UIElement element, Boolean value)
        {
            element.SetValue(LineBreakBeforeProperty, value);
        }

        public static Boolean GetLineBreakBefore(UIElement element)
        {
            return (bool)element.GetValue(LineBreakBeforeProperty);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size currentLineSize = new Size();
            Size panelSize = new Size();

            foreach (UIElement element in base.InternalChildren)
            {
                element.Measure(availableSize);
                Size desiredSize = element.DesiredSize;
                if (GetLineBreakBefore(element) || currentLineSize.Width + desiredSize.Width > availableSize.Width)
                {
                    //切換到新行,空間用完,或者通過設定附加依賴項屬性請求換行
                    panelSize.Width = Math.Max(currentLineSize.Width, panelSize.Width);
                    panelSize.Height += currentLineSize.Height;
                    currentLineSize = desiredSize;
                    //如果元素太寬無法使用最大行寬進行匹配,則只需要為其指定單獨的行,
                    if (desiredSize.Width > availableSize.Width)
                    {
                        panelSize.Width = Math.Max(desiredSize.Width, panelSize.Width);
                        panelSize.Height += desiredSize.Height;
                        currentLineSize = new Size();
                    }
                }
                else
                {
                    //添加到當前行,
                    currentLineSize.Width += desiredSize.Width;
                    //確保線條與最高的元素一樣高
                    currentLineSize.Height = Math.Max(desiredSize.Height, currentLineSize.Height);
                }
            }
            //回傳適合所有元素所需的大小,
            //通常,這是約束的寬度,高度基于元素的大小,
            //但是,如果一個元素的寬度大于面板的寬度,
            //所需的寬度將是該行的寬度,
            panelSize.Width = Math.Max(currentLineSize.Width, panelSize.Width);
            panelSize.Height += currentLineSize.Height;
            return panelSize;
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            int firstInLine = 0;
            Size currentLineSize = new Size();
            //積累高度
            double accumulatedHeight = 0;
            UIElementCollection elements = base.InternalChildren;
            for (int i = 0; i < elements.Count; i++)
            {
                Size desiredSize = elements[i].DesiredSize;
                if (GetLineBreakBefore(elements[i]) || currentLineSize.Width + desiredSize.Width > finalSize.Width)
                {
                    //換行
                    arrangeLine(accumulatedHeight, currentLineSize.Height, firstInLine, i);
                    accumulatedHeight += currentLineSize.Height;
                    currentLineSize = desiredSize;
                    if (desiredSize.Width > finalSize.Width)
                    {
                        arrangeLine(accumulatedHeight, desiredSize.Height, i, ++i);
                        accumulatedHeight += desiredSize.Height;
                        currentLineSize = new Size();
                    }
                    firstInLine = i;
                }
                else
                {
                    //繼續當前前行,
                    currentLineSize.Width += desiredSize.Width;
                    currentLineSize.Height = Math.Max(desiredSize.Height, currentLineSize.Height);
                }
            }
            if (firstInLine < elements.Count)
            {
                arrangeLine(accumulatedHeight, currentLineSize.Height, firstInLine, elements.Count);
            }
            return finalSize;
        }
        private void arrangeLine(double y, double lineHeight, int start, int end)
        {
            double x = 0;
            UIElementCollection children = InternalChildren;
            for (int i = start; i < end; i++)
            {
                UIElement child = children[i];
                child.Arrange(new Rect(x, y, child.DesiredSize.Width, lineHeight));
                x += child.DesiredSize.Width;
            }
        }
    }
}

呼叫的XAML代碼:

<Window x:
        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:local="clr-namespace:CustomElement"
        xmlns:customPanel="clr-namespace:CustomElement.Panels"
        mc:Ignorable="d"
        Title="CustomPanel" Height="450" Width="800">
    <Grid>
        <customPanel:CanvasClone>
            <TextBlock Text="asg" Canvas.Left="100" Canvas.Top="100"/>
        </customPanel:CanvasClone>
        <customPanel:WrapBreakPanel>
            <Button>No Break Here</Button>
            <Button>No Break Here</Button>
            <Button>No Break Here</Button>
            <Button>No Break Here</Button>
            <Button customPanel:WrapBreakPanel.LineBreakBefore="True" FontWeight="Bold" Content="Button with Break"/>
            <Button>No Break Here</Button>
            <Button>No Break Here</Button>
            <Button>No Break Here</Button>
            <Button>No Break Here</Button>
        </customPanel:WrapBreakPanel>
    </Grid>
</Window>

在MeasureOverride階段,主要是測量元素的位置和是否需要換行,在ArrangeOverride階段,每計算滿顯示一行的元素后,就開始繪制這一行,這里只需要了解著這個MeasureOverride和ArrangeOverride在干什么就行,目前這里不需要掌握,因為后面會講到串列虛擬化和資料虛擬化,會更詳細的講相關的設計內容,這里只要直到,有自定義面板可以自己設計串列的呈現,就可以了,

我創建了一個C#相關的交流群,用于分享學習資料和討論問題,這個propuev也在群檔案里,歡迎有興趣的小伙伴:QQ群:542633085

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/285410.html

標籤:WPF

上一篇:【翻譯】WPF中的資料系結運算式

下一篇:WPF 給類別庫設定設計時使用的資源字典

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more