I want to create a WPF user control exposing a property called InnerCaption. The code is below
XAML code
<UserControl x:Class="myControl.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock Name="_innerCaption" > Hello</TextBlock>
</Grid>
</UserControl>
CS code behind
namespace myControl
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public TextBlock InnerCaption
{
get { return (TextBlock)this.GetValue(InnerCaptionProperty); }
set { this.SetValue(InnerCaptionProperty, value); }
}
public static readonly DependencyProperty InnerCaptionProperty = DependencyProperty.Register(
"InnerCaption", typeof(TextBlock), typeof(UserControl1),new PropertyMetadata(false));
}
}
The question is: I want users to be able to customize the InnerCaption at design time such as: modifying its color, its font style … But I don’t know how Sigh I tried to use some kinds of Binding. But it’s futile. As I know, Binding only supports for binding to a property, right?
Show me a way pls! 🙁
There’re a simple solution to my problem above: how about the use of ContentPresenter control? Just a few line of code declaring your property (TextBlock, string, …) and then bind the control to the property. I tried and it works! 😀