WPF 自定义控件和直接内容支持 [英] WPF custom control and direct content support
问题描述
我对 WPF 还很陌生,有点卡住了,所以任何帮助将不胜感激.
I am fairly new to WPF, and am a bit stuck, so any help would be appreciated.
我正在尝试编写 WPF 自定义控件,该控件封装了我已经在使用的几个功能元素(即排序、过滤、标准菜单等),但封装在一个漂亮整洁的包中以避免重复.
I am trying to write WPF custom control that encapsulates several elements of functionality that I already having working (i.e sorting, filtering, standard menus, etc.), but in a nice neat package to avoid repetition.
无论如何我已经创建了自定义控件(基于控件),然后在 Generic.Xaml 中有以下内容
Anyway I have created the custom control (based on control), and then have the following in the 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:Controls.ListViewExtended">
<Style TargetType="{x:Type local:ListViewExtended}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ListViewExtended}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ListView>
<ListView.View>
<GridView>
<!-- Content goes here -->
</GridView>
</ListView.View>
</ListView>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
当我尝试添加 GridViewColumns(或任何控件)时,如下...
When I try to add GridViewColumns (or any control really), as below ...
<elv:ListViewExtended>
<GridView>
<GridViewColumn Width="140" Header="Column 1" />
<GridViewColumn Width="140" Header="Column 2" />
<GridViewColumn Width="140" Header="Column 3" />
</GridView>
</elv:ListViewExtended>
我收到...不支持直接内容"错误.
我已经创建了一个允许添加 GridView 的依赖属性(再次在下面),但它仍然不起作用.
I have created a dependancy property (again below) that allows the adding of GridView, but it still doesn't work.
public static DependencyProperty GridViewProperty;
public static string GridViewHeader(DependencyObject target)
{
return (string)target.GetValue(GridViewProperty);
}
public static void GridViewHeader(DependencyObject target, string value)
{
target.SetValue(GridViewProperty, value);
}
提前致谢
推荐答案
从 ListView
继承您的自定义控件,而不是从 Control
.这肯定会导致您更改模板,但我鼓励您阅读更多有关如何操作的文档(例如 Sacha Barber 的文章:创建和使用自定义 WPF 控件).
Inherit your custom control from the ListView
, not from the Control
. This would definitely cause you to change the template, but I encourage you to read more documentation on how to do it (e.g. Sacha Barber's article: Creating and consuming a custom WPF control).
祝你学习顺利!
这篇关于WPF 自定义控件和直接内容支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!