我有一个简单的控制 ContentControl 具有3个属性。

当我尝试执行控制control.transformtovisual()的控件时,我的问题就到了 MainContent. 。它总是带来 ArgumentNullException.

我的猜测是由于控件具有空父属性。有什么简单的方法可以解决这个问题吗?

C#

public static readonly DependencyProperty LabelTextProperty =
    DependencyProperty.Register("LabelText", typeof(string), typeof(LabelledControl), null);

public static readonly DependencyProperty ValidationContentProperty =
    DependencyProperty.Register("ValidationContent", typeof(object), typeof(LabelledControl), null);

public static readonly DependencyProperty MainContentProperty =
    DependencyProperty.Register("MainContent", typeof(object), typeof(LabelledControl), null);

XAML

<Style TargetType="local:LabelledControl">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="local:LabelledControl">

            <StackPanel Margin="0 10 0 0">
                <StackPanel Orientation="Vertical">
                    <dataInput:Label Content="{TemplateBinding LabelText}" FontWeight="Bold" FontSize="12" IsTabStop="False"/>
                    <ContentControl Content="{TemplateBinding ValidationContent}" IsTabStop="False"/>
                </StackPanel>
                <ContentControl x:Name="_contentControl" Content="{TemplateBinding MainContent}" IsTabStop="False"/>
            </StackPanel>

        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

没有正确的解决方案

其他提示

您是否尝试过使用 ContentPresenter 班级而不是 ContentControl 在您的ControlTemplate中的类以在模板中介绍这些属性?我不确定它是否与您的参数nullexception相关,但通常是 ContentControl 通过A暴露在模板上 ContentPresenter.

由于您的控件来自 ContentControlContentPresenter 将为您自动将内容和ContentTemplate属性绑定到设置的内容属性。您也可以手动绑定 ContentPresenter 到您的验证属性属性。

我不确定为什么在基础时定义主以属性 ContentControl 已经为您提供了一个可以使用的内容属性,也许这是您要揭示的第二部分内容。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top