0

I actually have two questions:

  1. How do I modify a custom control that inherits from a FrameworkElement that only holds one child (e.g. Page or ContentControl) so that it holds multiple children (like a Panel)? Can this be done from the class definition?

  2. How do I bind the children of a custom control to a Panel object (e.g WrapPanel) defined in the template of the custom control?

I'm not even sure if it is possible, but I want to create a custom control that behaves like a Page, or may even inherit from Page, but allows me to enter in children in XAML. For example, I would like the XAML for generating my custom control to look like this:

<CustomPage CustomAttribute="blah">
    <TextBox/>
    <TextBlock Text="hehe"/>
    <Label Content="ha!"/>
</CustomPage>

I want to define in the style that a WrapPanel displays the children. Something like this:

<Style TargetType="{x:Type CustomPage}">
    <Style.Setters>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CustomPage}">
                    <WrapPanel/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

I would replace the WrapPanel with a ContentPresenter except that I want the ContentPresenter to behave like a WrapPanel.

I hope this is specific enough.

4

2 回答 2

0

实际上,您可以使用 ItemsControl 完全按照您的意愿行事。

    <ItemsControl>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.Items>
            <TextBox/>
            <TextBlock Text="hehe"/>
            <Label Content="ha!"/>
        </ItemsControl.Items>
    </ItemsControl>

希望有帮助

编辑:您可以使用上面的 ItemsControl 作为页面中的根元素来获得页面的功能。

于 2013-05-02T23:58:45.693 回答
0

谢谢你,穆尔凯乌斯。你让我朝着正确的方向前进。

最后,我创建了一个继承自 Page 的自定义控件,并为其定义了一个 ContentProperty。这显然覆盖了基类的 ContentProperty。这也适用于任何控制类型。这是我的代码:

    [ContentProperty("Children")]
    class CustomPage : System.Windows.Controls.Page
    {
        ObservableCollection<UIElement> children = new ObservableCollection<UIElement>();
        public ObservableCollection<UIElement> Children { get { return children; } set { children = value; } }
    }

然后,我在 Themes/Generic.xaml 文件中为我的控件定义了模板,并使用我的自定义 ContentProperty 作为源使用 ItemsControl:

    <Style TargetType="local:CustomPage">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ItemsControl ItemsSource="{Binding Children,
                                                RelativeSource={RelativeSource TemplatedParent}}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我希望这可以帮助其他正在寻找解决同一问题的方法的人。再次感谢您,Murkaeus,您的帮助!

编辑:我应该警告大家,如果你使用这种方法,你的所有数据绑定都会由于某种原因丢失,我通过进一步的实验发现了这一点。我最终放弃了,只是将一个面板指定为我的自定义页面对象的子项。

于 2013-05-10T17:08:47.107 回答