我希望创建自定义控件并添加Info和InfoTemplate属性。
我在generic.xaml中使用ContentPresenter为Info属性定义ControlTemplate。
当我不使用InfoTemplate时,它能正常工作,但是当我应用ItemTemplate时,内容会表示为类名字符串。同样的模板应用于GroupBox时则像预期一样工作。我做错了什么?我需要在OnApplyTemplate中添加一些额外的代码吗?
下面是我的应用程序和源代码的打印屏幕。红色边框是GroupBox,蓝色是我的控件。绿色边框是DataTemplate的一部分。
在这种情况下,GroupBox.Header的行为类似于我的MyCustomControl,并显示文本而不是控件。所以问题是:我应该在我的控件事件中实现什么来达到我想要的效果?
我在generic.xaml中使用ContentPresenter为Info属性定义ControlTemplate。
当我不使用InfoTemplate时,它能正常工作,但是当我应用ItemTemplate时,内容会表示为类名字符串。同样的模板应用于GroupBox时则像预期一样工作。我做错了什么?我需要在OnApplyTemplate中添加一些额外的代码吗?
下面是我的应用程序和源代码的打印屏幕。红色边框是GroupBox,蓝色是我的控件。绿色边框是DataTemplate的一部分。
编辑: 为了测试,我创建了一个名为MyGroupBox的类,继承自GroupBox,并重写了OnHeaderChanged方法。
public class MyGroupBox : GroupBox
{
protected override void OnHeaderChanged(object oldHeader, object newHeader)
{
//base.OnHeaderChanged(oldHeader, newHeader);
}
}
在这种情况下,GroupBox.Header的行为类似于我的MyCustomControl,并显示文本而不是控件。所以问题是:我应该在我的控件事件中实现什么来达到我想要的效果?
MyCustomControl.cs
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication7
{
public class MyCustomControl : ContentControl
{
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
public object Info
{
get { return (object)GetValue(InfoProperty); }
set { SetValue(InfoProperty, value); }
}
public DataTemplate InfoTemplate
{
get { return (DataTemplate)GetValue(InfoTemplateProperty); }
set { SetValue(InfoTemplateProperty, value); }
}
public static readonly DependencyProperty InfoProperty =
DependencyProperty.Register(nameof(Info), typeof(object), typeof(MyCustomControl), new PropertyMetadata(null));
public static readonly DependencyProperty InfoTemplateProperty =
DependencyProperty.Register(nameof(InfoTemplate), typeof(DataTemplate), typeof(MyCustomControl), new PropertyMetadata(null));
}
}
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication7">
<Style TargetType="{x:Type local:MyCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyCustomControl}">
<StackPanel>
<TextBlock FontWeight="Bold">Info</TextBlock>
<ContentPresenter ContentSource="Info"/>
<TextBlock FontWeight="Bold">Content</TextBlock>
<ContentPresenter/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
MainWindow.xml
<Window x:Class="WpfApplication7.MainWindow"
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:WpfApplication7"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
DATA_CONTEXT
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="dataTemplate">
<Border BorderBrush="Green" BorderThickness="5">
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</Window.Resources>
<StackPanel>
<Border BorderBrush="Red" BorderThickness="4">
<GroupBox HeaderTemplate="{StaticResource dataTemplate}">
<GroupBox.Header>
<TextBlock Text="{Binding}"/>
</GroupBox.Header>
</GroupBox>
</Border>
<Border BorderBrush="Blue" BorderThickness="4">
<local:MyCustomControl InfoTemplate="{StaticResource dataTemplate}">
<local:MyCustomControl.Info>
<TextBlock Text="{Binding}"/>
</local:MyCustomControl.Info>
My content
</local:MyCustomControl>
</Border>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Windows;
namespace WpfApplication7
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}