سؤال

لديّ تحكم بسيط مشتق من ContentControl مع 3 خصائص.

مشكلتي تأتي عندما أحاول إجراء عنصر تحكم. MainContent. إنه دائمًا ما يظهر ArgumentNullException.

أظن أن هذا بسبب التحكم في خاصية الوالدين الفارغة. هل هناك طريقة بسيطة للتغلب على هذا؟

ج#

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 لتقديم تلك الخصائص داخل القالب؟ لست متأكدًا مما إذا كان مرتبطًا بـ AgnumentNullexception ، ولكن عادةً ما يكون محتوى أ ContentControl مكشوف على القالب عبر أ ContentPresenter.

لأن سيطرتك مستمدة من ContentControl ال ContentPresenter سيقوم تلقائيًا بربط خصائص المحتوى و ContentTemplate لك بكل ما تم تعيين خاصية المحتوى. يمكنك أيضًا ربط خاصية المحتوى يدويًا ContentPresenter إلى خاصية التحقق من الصحة.

لست متأكدًا من سبب تعريف خاصية MainContent عند القاعدة ContentControl يمنحك بالفعل خاصية محتوى لاستخدامها ، وربما يكون هذا محتوى ثانٍ تحاول فضحه.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top