2

I am creating some custom user controls for WPF. This user controls contain custom dependency properties so I can fill them in the designer.

One of this properties is called "InnerUserControlType". This dependency proeprty is a custom enumeration containing some values like TextBox, ComboBox, Label, CheckBox, etc.

I would like to be able to set this property in my XAML pages in the designer and then see the user control change the displayed inner control depending on the property.

How should I implement this? The grid which will contain the inner control in my user control is a normal field, so it cannot be accessed from a static method property (like the dependency properties).

I want it to have it working in the designer so the designers can work easily.

Thanks a lot!

4

1 回答 1

0

将 a 放ContentControl入网格中,将Content属性绑定到UserControlvia 相对源

<ContentControl Content="{Binding RelativeSource={RelativeSource FindAnsector,
                                  AncestorType={x:Tyle myNamespace:MyControl}}}"

并制作一个DataTemplateSelector将检查的值InnerUserControlType并将返回一个适当的数据模板,其中包含在属性中要求的控件。

根据您的方案,您可能需要确保数据模板中的控件具有正确的数据上下文。如果控件的数据上下文应该与用户控件的数据上下文相同,则在数据模板的根元素上,为数据上下文添加相对绑定。类似于(对于文本框数据模板):

<TextBox DataContext="{Binding DataContext,
                               RelativeSource={RelativeSource FindAnsector,
                               AncestorType={x:Tyle myNamespace:MyControl}}}"
 ......
</TextBox>

编辑:

我只注意到 wpf 标签并错过了 silverlight 一个。在 Silverlight 中,您没有...TemplateSelector属性,因此请改用转换器。

于 2012-05-22T11:00:02.503 回答