wpf 附加属性使用

我有个自定义控件,控件里面想要使用外部的一些控件,这个时候就可以使用占位符+附加属性

 

这是我的资源字典,其中contentpresenter为占位符,然后Header是我定义的附加属性

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,10,0">
<TextBlock Text="{TemplateBinding Title}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" />

<ContentPresenter ContentSource="Header"/>
</StackPanel>

 

我的附加属性:

public object Header
{
get { return (object)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}

// Using a DependencyProperty as the backing store for Header. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(object), typeof(CustomWnd), new PropertyMetadata(null));

 

前台使用:

<wnd:CustomWnd
x:Class="TridentClusterUI.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TridentClusterUI="clr-namespace:TridentClusterUI"
xmlns:cal="http://www.caliburn.org"

XXXXX

>

 

<wnd:CustomWnd.Header>
<TextBlock Text="123"/>
</wnd:CustomWnd.Header>



这个时候就可以随便写什么,把textblock替换成grid也行

posted @ 2025-01-13 19:33  无锡佬帅鸽  阅读(31)  评论(0)    收藏  举报