独自のカスタム レイアウト コントロールを作成しようとしているので、次のような UserControl を作成しました。
public partial class KTopContentBottomLayout : UserControl
{
Orientation _Orientation = Orientation.Vertical;
public Orientation Orientation { get { return this._Orientation; } set { this._Orientation = value; } }
public UIElementCollection Children
{
get
{
return this.LayoutRoot.Children;
}
}
public KTopContentBottomLayout()
{
InitializeComponent();
CreateProperLayout();
}
//Some other code that creates the layouts
}
次に、このコントロールを使用する MainPage.xaml で:
<Grid x:Name="LayoutRoot" Background="White">
<local:KTopContentBottomLayout Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<local:KTopContentBottomLayout.Children>
<StackPanel>
<c1:C1RichTextBoxToolbar RichTextBox="{Binding ElementName=MyRichTextBox}"/>
</StackPanel>
<c1:C1RichTextBox x:Name="MyRichTextBox" ReturnMode="HardLineBreak" />
</local:KTopContentBottomLayout.Children>
</local:KTopContentBottomLayout>
</Grid>
これまでのところ、すべてが正常にコンパイルされ、適切に表示されます。しかし、Loaded イベントでコントロールを使用しようとすると:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
this.MyRichTextBox.GotFocus += MyRichTextBox_GotFocus;
this.MyRichTextBox.LostFocus += MyRichTextBox_LostFocus;
}
オブジェクトはMyRichTextBoxnullを返します...
カスタム レイアウト コントロールの外にコントロールを配置しようとしましたが、正常に動作しています。それで、私は何を間違えましたか?
編集:
MyRichTextBoxから にアクセスすると、 に正しくKTopContentBottomLayoutアクセスできることに気付きました。MyRichTextBox
つまり、Children.First() などを使用してプロパティ/アクセスを作成します。
MainPage.xamlしかし、直接アクセスするMyRichTextBoxと null が返される理由がわかりません。
KTopContentBottomLayoutasに置き換えると直接Grid/StackPanelアクセスできるようMyRichTextBoxになるのですが、その理由は何ですか?