0

我可能只是错过了一些东西,或者今天可能不是我的一天,但我正在尝试做的事情一直失败。

我有一个名为 MyContentControl 的自定义控件。它看起来像这样:

public class MyContentControl : ContentControl
{
    static MyContentControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyContentControl), new FrameworkPropertyMetadata(typeof(MyContentControl)));
    }

    public DockPanel DifferentLook
    {
        get;
        set;
    }

    public string Txt
    {
        get;
        set;
    }

    protected override void OnInitialized(EventArgs e)
    {
        if (this.DifferentLook != null)
        {
            this.Content = this.DifferentLook;
        }

        Binding b = new Binding("Txt");
        b.Source = this;
        this.SetBinding(ContentProperty, b);
        base.OnInitialized(e);
    }
}

这是它的主题:

<Style TargetType="{x:Type local:MyContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyContentControl}">
                <Border Background="{TemplateBinding Background}" Margin="{TemplateBinding Padding}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="True">
                    <ContentPresenter VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是主窗口:

<local:MyContentControl Txt="texty text">
    <local:MyContentControl.DifferentLook>
        <DockPanel>
            <TextBlock Text="Content = " DockPanel.Dock="Left"/>
            <ContentPresenter DockPanel.Dock="Left"/>
        </DockPanel>
    </local:MyContentControl.DifferentLook>
</local:MyContentControl>

当没有指定“DifferentLook”时,我希望控件使用在默认 ControlTemplate 中定义的 ContentPresenter。

如果我设置了 DifferentLook,那么它应该以不同的外观显示控件。

请参阅 OnInitalized 方法。

现在的问题是,当我应用 DifferentLook 时,DifferentLook.ContentPresenter 似乎不起作用。

为什么 DifferentLook.ContentPresenter 没有正确应用内容?

窗口上的输出是“texty text”,但应该是“content = texty text”。

编辑:这是轻型版本。我创建了这个并尽可能简单地说明问题。实际上,自定义控件有点大,用户可能不会覆盖 ControlTemplate。

你们知道如何根据给定的要求解决这个问题吗?

4

1 回答 1

0

Buddy I am not quite agreed with the approach you are taking here. But I can point out what you are doing wrong and why you are not able to see your DockPanel.

Since you have defined the ControlTemplate for your ContentControl having ContentPresenter, then any Content you set on your ContentControl will be placed inside the ContentPresenter of your ControlTemplate defined in style whether it is the Content set by Binding or Directly placed inside on control (like DockPanel here)

In your example, OnInitialized() function first set the Content to the DockPanel and then immediatly set Binding on Content. The moment Binding is set it will override the previously set DockPanel with the Source value which is string Txt(texty text).

So if you remove the Binding setting from Oninitialized you will see your DockPanel. But wait there is one more thing. Inside the Content of your ContentPresenter with Content bound as Content="{Binding}" which will make your programme to go into infinite recursion as it will try to bind it to self and on and on. So you will have to set the Content Binding of inner ContentPresenter to some property of Parent ContentControl Context. So just for testing try removing the content binding from inner contentpresenter and give some static value.

于 2013-10-23T09:34:13.427 回答