ContentControl内に配置すると、コントロールの親はヌルです
-
29-09-2019 - |
質問
由来する簡単なコントロールがあります ContentControl
3つのプロパティを備えています。
私の問題は、内部に配置されたコントロールを使用してcontrol.transformtovisual()を実行しようとするときに発生します MainContent
. 。それは常にandを持ちます 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内のクラステンプレート内でそれらのプロパティを表示するには?それがあなたの引数に関連しているかどうかはわかりませんが、通常はの内容 ContentControl
テンプレートでaを介して露出しています ContentPresenter
.
あなたのコントロールは派生しているので ContentControl
ContentPresenter
コンテンツプロパティとコンテンツプロパティが設定されているものにコンテンツとコンテンツプロパティを自動的にバインドします。あなたは、のコンテンツプロパティを手動で結合することもできます ContentPresenter
ValidationContentプロパティに。
基地のときにメインコンテンツプロパティを定義している理由がわかりません ContentControl
すでに使用するコンテンツプロパティを提供していますが、それはあなたが公開しようとしている2番目のコンテンツです。
所属していません StackOverflow