views:

118

answers:

1

Hello, I'm new to WFP and try to learn it via different examples.

Right now I want to create a custom user control, which have button inside it (among other controls). I want to expose Buttons Content property as User Controls own.

User Control:

<DockPanel LastChildFill="True">
    <Button Name="ClickButton" DockPanel.Dock="Right" Focusable="False"/>
    <TextBox Name="TextBox">lorem ipsum dolor set amet</TextBox>
</DockPanel>

I want to change Buttons Content in Main Application window, like that:

<my:ButtonTextBox Name="mtb1" Grid.Column="0" Grid.Row="0">Some text</my:ButtonTextBox>

there Some text, is the text to be written over button...

How can I achieve that? Thanks!

+1  A: 

Your UserControl should define its own dependency property for the button content:

public static readonly DependencyProperty ButtonContentProperty = ...;

Then bind your UserControl's Button to that property:

<Button Name="ClickButton" Content="{Binding ButtonContent}" .../>

Then you can consume it as follows:

<my:ButtonTextBox ButtonContent="Some text" .../>

If you want to be able to do this:

<my:ButtonTextBox ...>Some text</my:ButtonTextBox>

you will need to specify the ContentPropertyAttribute on your UserControl:

[ContentProperty("ButtonContent")]
public partial class ButtonTextBox

HTH,
Kent

Kent Boogaart
thank you. it worked after some minor (and I think for most ppl trivial) additions
David