C# 将ContentPresenter用于自定义控件(Thumb)
我使用Thumb控件的DragDelta创建了一个允许拖动的自定义控件。我希望能够在自定义控件ContentPresenter中插入形状、图像或文本块 CustomControl.xaml(拇指)C# 将ContentPresenter用于自定义控件(Thumb),c#,wpf,C#,Wpf,我使用Thumb控件的DragDelta创建了一个允许拖动的自定义控件。我希望能够在自定义控件ContentPresenter中插入形状、图像或文本块 CustomControl.xaml(拇指) main window.xaml <Window x:Class="StackOverflow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmln
main window.xaml
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StackOverflow">
<local:CustomControl>
<!--Shape, Image or TextBlock-->
</local:CustomControl>
</Window>
艾德·普朗基特的功劳 CustomControl.xaml.cs(拇指)
由于
Content
属性是Object
,因此您可以将任何内容放入ContentControl
:可视化树元素、字符串、带有隐式DataTemplate的viewmodel
(在这种特殊情况下非常牵强,但这是原理)——您可以命名
MyThumb.xaml
<Thumb
x:Class="ThumbTest.MyThumb"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:thumb="clr-namespace:ThumbTest"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
>
<Thumb.Template>
<ControlTemplate TargetType="thumb:MyThumb">
<ContentPresenter
/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
MyThumb.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
namespace ThumbTest
{
[ContentProperty("Content")]
public partial class MyThumb : Thumb
{
public MyThumb()
{
InitializeComponent();
}
#region Content Property
public Object Content
{
get { return (Object)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(Object), typeof(MyThumb),
new PropertyMetadata(null));
#endregion Content Property
}
}
由于
Thumb
不是从ContentControl
派生的,因此我认为您必须为类指定类型为Object
的Content
依赖属性,并为自定义控件类指定这两个属性:[ContentProperty(“Content”)][DefaultProperty(“Content”)]
…并提供控制模板TargetType=“local:CustomControl”
,不管实际调用的是什么thumb类。无需将其设置为FrameworkElement
<代码>对象
也适用于可视树元素,但也允许您使用隐式数据模板创建字符串或视图模型。
<Thumb
x:Class="ThumbTest.MyThumb"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:thumb="clr-namespace:ThumbTest"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
>
<Thumb.Template>
<ControlTemplate TargetType="thumb:MyThumb">
<ContentPresenter
/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
<Thumb.Template>
<ControlTemplate TargetType="thumb:MyThumb">
<ContentControl
Content="{Binding Content, RelativeSource={RelativeSource AncestorType=thumb:MyThumb}}"
/>
</ControlTemplate>
</Thumb.Template>
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
namespace ThumbTest
{
[ContentProperty("Content")]
public partial class MyThumb : Thumb
{
public MyThumb()
{
InitializeComponent();
}
#region Content Property
public Object Content
{
get { return (Object)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(Object), typeof(MyThumb),
new PropertyMetadata(null));
#endregion Content Property
}
}