Answered by:
UserControl Binding

Question
-
Hey,
I have a problem with UserControls and bindings:
I want to bind to a UserControl called HeaderedTextBlock using a converter like this:
<controls:HeaderedTextBlock Header="Header 2" Text="{Binding Converter={StaticResource converter}, ConverterParameter=Title}" />
>
The converter returns a string object..
This is HeaderedTextBlock.cs:
public sealed partial class HeaderedTextBlock : UserControl { public string Header { get { return header.Text; } set { header.Text = value; } } public string Text { get { return content.Text; } set { content.Text = value; this.Visibility = (string.IsNullOrWhiteSpace(value)) ? Visibility.Collapsed : Visibility.Visible;//hide if empty } } public HeaderedTextBlock() { this.InitializeComponent(); } }
The UserControl works fine as long as you do not bind one of its properties.
I set a breakpoint in the converter, but the exception occurs even before the converter is called. The very same Text="..." works fine with a TextBlock control.
How do I have to modify the UserControl to support binding?
thanks in advance,
~theCake
Life is unsure - always eat the dessert first!
- Edited by Michael Osthege Wednesday, August 8, 2012 9:27 AM
Wednesday, August 8, 2012 9:21 AM
Answers
-
Try making the target property you want to bind to a dependency property:
public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyUserControl1), new PropertyMetadata(string.Empty));
- Proposed as answer by Can Bilgin Wednesday, August 8, 2012 11:23 AM
- Marked as answer by Michael Osthege Saturday, August 11, 2012 7:55 PM
Wednesday, August 8, 2012 10:09 AM -
Thanks!
I found this to be the best approach to bind to the DependencyProperty "Text":
<UserControl ... x:Name="self"> ... <TextBlock Text="{Binding Text, ElementName=self}" /> ... </UserControl>
cheers,
~theCake
Life is unsure - always eat the dessert first!
- Marked as answer by Michael Osthege Saturday, August 11, 2012 8:00 PM
- Edited by Michael Osthege Saturday, August 11, 2012 8:01 PM
Saturday, August 11, 2012 8:00 PM
All replies
-
Try making the target property you want to bind to a dependency property:
public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyUserControl1), new PropertyMetadata(string.Empty));
- Proposed as answer by Can Bilgin Wednesday, August 8, 2012 11:23 AM
- Marked as answer by Michael Osthege Saturday, August 11, 2012 7:55 PM
Wednesday, August 8, 2012 10:09 AM -
Thanks!
I found this to be the best approach to bind to the DependencyProperty "Text":
<UserControl ... x:Name="self"> ... <TextBlock Text="{Binding Text, ElementName=self}" /> ... </UserControl>
cheers,
~theCake
Life is unsure - always eat the dessert first!
- Marked as answer by Michael Osthege Saturday, August 11, 2012 8:00 PM
- Edited by Michael Osthege Saturday, August 11, 2012 8:01 PM
Saturday, August 11, 2012 8:00 PM