Bind gui controls within a UserControl to another UserControl and its containing controls in WPF
I have UserControl1 which is a FormatButtonBar with format buttons AND I have UserControl2 which is a DataGrid with RichTextBoxes as cell editors.
I want to reuse UserControl1 at different places of my application.
This is what I want to achieve with pseudo code:
<UserControl1>
<ToggleButton Content="bold" IsChecked="{Binding IsTextBold}" Command="EditingCommands.ToggleB开发者_开发百科old" CommandTarget="{Binding ElementName=UserControl2.MyRichTextBox}" />
</UserControl1>
<UserControl2>
<DataGrid>
<DataGridCell x:Name="MyRichTextBox" />
</DataGrid>
</UserControl2>
Do you know how the binding must look like?
You will need to define [ContentProperty( "CustomContent" )]
on your UserControl class by pointing it to a custom UIElement dependency property. Then in your UserControl xaml, add a <ContentControl>
and bind its Content
property to your custom property like so:
<ContentControl
Content="{Binding ElementName=myUserControl, Path=CustomContent}" />
精彩评论