通过属性的 ContentControl 模板 [英] ContentControl Template through Property

查看:27
本文介绍了通过属性的 ContentControl 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ControlTemplate DependencyProperty(名为 MyItemTemplate)的用户控件.

I have a Usercontrol with a ControlTemplate DependencyProperty (named MyItemTemplate).

public ControlTemplate MyContentControl
        {
            get { return (ControlTemplate)GetValue(MyContentControlProperty); }
            set { SetValue(MyContentControlProperty, value); }
        }
        public static readonly DependencyProperty MyContentControlProperty =
            DependencyProperty.Register("MyContentControl", typeof(ControlTemplate), typeof(MyScroll),
              new PropertyMetadata(new ControlTemplate()));

在我的 UserControl 的 xaml 中,我想使用MyItemTemplate"作为这样的 ContentControl 的模板:

In the xaml of my UserControl I want to use the "MyItemTemplate" as a template for a ContentControl like that :

<ContentControl x:Name="MyContentControl" Template="{Binding MyItemTemplate}" />

我知道 Template="{Binding MyItemTemplate}" 是错误的,但我不知道该怎么做...

I know that the Template="{Binding MyItemTemplate}" is wrong, but I wonder how to do it...

谢谢

推荐答案

您可以使用 RelativeSource 绑定来引用 UserControl 上的自定义 DependencyProperty

You can use a RelativeSource binding to reference a custom DependencyProperty on your UserControl

<ContentControl Template="{Binding 
    RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}, 
    Path=MyItemTemplate}" />

编辑

如果您在 Silverlight 4.0 或更低版本中工作,该版本不支持 RelativeSource 绑定,则为您的 UserControl 标记提供一个名称并使用一个 ElementName 绑定

If you're working in Silverlight 4.0 or lower, which doesn't support RelativeSource bindings, then give your UserControl tag a Name and use an ElementName binding

<UserControl x:Name="MyUserControl" ...>
    <ContentControl Template="{Binding ElementName=MyUserControl, Path=MyItemTemplate}" />
</UserControl>

这篇关于通过属性的 ContentControl 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆