網站首頁 編程語言 正文
作為一個WPF控件開發者,我在工作中經常遇到如本文標題所示的問題。其實,這個問題并不是很難,只是在操作上有些繁瑣。本文將嘗試對這個問題進行解答,并且對相關的一些技術細節加以探討。
先從我遇到的一個典型的問題開始吧:寫一個MyElement類,要求如下:
- 從FrameworkElement繼承
- 增加一個Button到它的VisualTree上
在Visual上有一個AddVisualChild方法,相信很多剛接觸這個方法的同學們(好吧,至少我是這樣)都會“顧名思義”地認為這個方法就可以解決本文的問題。再加上MSDN上也給出了一個例子來“火上澆油”一把。于是,一陣竊喜之后,我興奮地敲出了以下代碼:
class MyElement : FrameworkElement
{
private Button _button = new Button() { Content = "I'm a Button!"};
public MyElement()
{
this.AssembleVisualChildren();
}
private void AssembleVisualChildren()
{
this.AddVisualChild(this._button);
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
protected override Visual GetVisualChild(int index)
{
return this._button ;
}
}
然后將這個MyElement加入測試窗口,代碼如下:
<Window
x:Class="AddVisualChildTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:AddVisualChildTest"
WindowStartupLocation="CenterScreen"
Title="Window1" Height="300" Width="300">
<Grid>
<loc:MyElement Margin="10"/>
</Grid>
</Window>
運行后的結果如下:
空空如也!嗯,被忽悠了。一陣失落、打擊之后,我的好奇心被激發了:這是為什么呢?于是我狂找資料,終于被我發現了:
實際上,在上面這個例子中,AddVisualChild這個方法只是在MyElement和Button之間建立起了一種VisualTree上的父子關系,但是并沒有將Button掛接到MyElement的VisualTree上,所以最終我們沒有在屏幕上看到這個Button。
為了將Button真正掛接到MyElement的VisualTree上,還需要額外做一件事情:在VisualTree上為這個Button分配空間并且指定位置,這個過程叫做Layout。此過程分兩個部分:一個是Measure,另一個是Arrange。這兩個過程在FrameworkElement上對應著兩個方法:MeasureOverride和ArrangeOverride方法。具體做法如下:
protected override Size MeasureOverride(Size availableSize)
{
if (this.VisualChildrenCount > 0)
{
UIElement child = this.GetVisualChild(0) as UIElement;
Debug.Assert(child != null); // !Assert
child.Measure(availableSize);
return child.DesiredSize;
}
return availableSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
Rect arrangeRect = new Rect()
{
Width = finalSize.Width,
Height = finalSize.Height
};
if (this.VisualChildrenCount > 0)
{
UIElement child = this.GetVisualChild(0) as UIElement;
Debug.Assert(child != null); // !Assert
child.Arrange(arrangeRect);
}
return finalSize;
}
再次運行程序:
目標實現。
由此,我們可以總結出這個問題的解決方案如下:
在MyElement的構造器中調用AddVisualChild方法;
重寫VisualChildCount屬性;
重寫GetVisualChild方法;
重寫MeasureOverride方法;
重寫ArrangeOverride方法;?
另外,WPF在此問題的解決上也為開發者提供了一些必要的幫助。就我所知的,有如下幾個內容:
1、Panel
還是本文開始提到的問題,只不過要將其中的FrameworkElement換為Panel。除了上面所提到的方法,Panel為我們提供了更加方便的實現方式。代碼如下:
class MyElement : Panel
{
private Button _button = new Button() { Content = "I'm a Button!" };
public MyElement()
{
this.Children.Add(_button);
}
protected override Size MeasureOverride(Size availableSize)
{
if (this.VisualChildrenCount > 0)
{
UIElement child = this.GetVisualChild(0) as UIElement;
Debug.Assert(child != null); // !Assert
child.Measure(availableSize);
return child.DesiredSize;
}
return availableSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
Rect arrangeRect = new Rect()
{
Width = finalSize.Width,
Height = finalSize.Height
};
if (this.VisualChildrenCount > 0)
{
UIElement child = this.GetVisualChild(0) as UIElement;
Debug.Assert(child != null); // !Assert
child.Arrange(arrangeRect);
}
return finalSize;
}
}
之所以能這樣做的原因是Panel已經替我們將如下幾個工作封裝在了UIElementCollection(Panel的Children屬性)中:
AddVisualChild
VisualChildCount
GetVisualChild
2、VisualCollection
另外,在這個過程中,我們還可以使用一個叫做VisualCollection的類來作為所有 Visual Child的容器。這個容器構造的時候需要一個Visual類型的Parent,然后在添加、刪除Visual Child的時候,它的相應方法(Add,Remove)就會幫助我們自動調用Parent的AddVisualChild和RemoveVisualChild方法。如此一來,我們的工作量又減少了。
原文鏈接:https://www.cnblogs.com/AaronLu/archive/2009/11/09/1599348.html
相關推薦
- 2022-12-15 Tensorflow2.1?MNIST圖像分類實現思路分析_python
- 2022-05-31 Python必備技巧之Pandas數據合并函數_python
- 2022-05-10 fatal: Not a git repository (or any of the parent
- 2022-06-28 C++哈希表之線性探測法實現詳解_C 語言
- 2022-11-12 docker修改容器內存大小的實現方式_docker
- 2022-09-13 GO語言包管理工具go?mod以及包詳解_Golang
- 2022-05-06 golang-操作sqlite3增刪改查
- 2022-06-09 ASP.NET?Core記錄日志_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支