WPF自定义控件和直接内容支持

mmm*_*mmm 1 wpf wpf-controls

我是WPF的新手,我有点卡住了,所以任何帮助都会受到赞赏.

我正在尝试编写WPF自定义控件,它封装了我已经工作的几个功能元素(即排序,过滤,标准菜单等),但是在一个漂亮的整齐包中以避免重复.

无论如何,我已经创建了自定义控件(基于控件),然后在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>
Run Code Online (Sandbox Code Playgroud)

当我尝试添加GridViewColumns(或任何控件)时,如下所示...

<elv:ListViewExtended>
   <GridView>
        <GridViewColumn Width="140" Header="Column 1" />
        <GridViewColumn Width="140" Header="Column 2" />
        <GridViewColumn Width="140" Header="Column 3" />
   </GridView>
</elv:ListViewExtended>
Run Code Online (Sandbox Code Playgroud)

我得到"...不支持直接内容"错误.

我创建了一个依赖属性(再次在下面),允许添加GridView,但它仍然无法正常工作.

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); 
} 
Run Code Online (Sandbox Code Playgroud)

提前致谢

msw*_*cki 18

您只需指定ContentPropertyAttribute即可.

[ContentProperty("MainContent")]
public class GroupPanel : Control
{
    public GroupPanel()
    {
        DefaultStyleKey = typeof(GroupPanel);
    }

    public object MainContent
    {
        get { return GetValue(MainContentProperty); }
        set { SetValue(MainContentProperty, value); }
    }

    public static readonly DependencyProperty MainContentProperty =
        DependencyProperty.Register("MainContent", typeof(object), typeof(GroupPanel), null);
}
Run Code Online (Sandbox Code Playgroud)

参考:http://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute(v = vs.90).aspx


Hub*_*aaa 5

我在我们的项目中使用了这个简单的解决方案来支持自定义控件中的直接内容:

向您的项目中添加“ CustomControl”,并从类“ UserControl”而不是“ Control”派生此控件:

public class MyCustomControl: UserControl
{
    static MyCustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), 
            new FrameworkPropertyMetadata(typeof(MyCustomControl)));
    }
}
Run Code Online (Sandbox Code Playgroud)

当您向项目中添加CustomControl时,Visual Studio(我正在使用2012)会自动添加文件夹“ Themes”,其中包括名为“ Generic.xaml”的文件。该文件包含一个ResourceDictionary,用于定义CustomControl的样式(模板)。

您将找到CustomControl的基本模板,该模板已经用作DefaultStyle。为了获得直接的内容支持,请将ContentPresenter放置在带有父内容绑定的此模板内的某个位置:

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

现在可以将内容添加到CustomControl中:

<Window x:Class="MyApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:MyApplication"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <controls:MyCustomControl>
                <TextBlock>Hello</TextBlock>
        </controls:MyCustomControl>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!