views:

39

answers:

1

Hello

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.ToggleBold" CommandTarget="{Binding ElementName=UserControl2.MyRichTextBox}" />
</UserControl1>


<UserControl2>
<DataGrid>
   <DataGridCell x:Name="MyRichTextBox" />
</DataGrid>
</UserControl2> 

Do you know how the binding must look like?

A: 

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}" />
chaiguy
your contentcontrol confuses me, why a contentcontrol here?? I have actually 2 UserControls...
whiteSox
Perhaps I misunderstood the question, but are you not trying to create a UserControl that you can place elements within in your usage of it? The ContentControl is simply a lightweight container that can be set to contain another UIElement. UserControls have a "bug" (or at least an annoyance) where if you set their content directly, you wipe out any custom definitions in the UserControl, defeating the entire point of the UserControl, really. This is why you need to define a custom UIElement DependencyProperty and bind to it. Setting the [ContentProperty] attribute is just a syntactic shortcut.
chaiguy
Note also if you want your UserControl to support *multiple* content elements, you'll need to declare a DependencyProperty of a collection (UIElementCollection) and set it as the [ContentProperty]. If the ContentProperty is not a collection, you won't be able to add multiple content elements in XAML.
chaiguy