Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to create a WPF user control exposing a property called InnerCaption. The code is below
XAML code
XML
<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
C#
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! :(
Posted
Updated 24-Jan-11 20:16pm
v2

1 solution

At this point, your functionality is no different from that of TextBlock. That renders your problem highly artificial. If you add some functionality, the answer may depend on that functionality.

You should better avoid exposing your inner control as a public property. Basically, you need to reproduce same properties as your inner UIElement, such as Text, FontSize, Foregroung, etc., probably under different names: InnerTextText (sorry, clumsy name :-)), InnerTextFontSize, InnerTextForegroung, respectively. The implementation of these properties' setter and getter should transparently read/assign respective properties of the inner UIElement.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Jan-11 2:30am    
By the way, do you know how to use custom/user control in XAML? (Adding your namespace, writing a corresponding tag?) Asking just in case -- could be a part of your problem...
Le Duc Anh 25-Jan-11 2:34am    
Yeah, I use the code above to demonstrate a more complicated control I'm creating. Your idea of reproducing properties under different names is totally acceptable :) However, by doing such way, we have to implement many lines of C# code. :B
Sergey Alexandrovich Kryukov 25-Jan-11 13:59pm    
I suggest you accept my answer. Of course you can consider alternatives, but the encapsulated approach I offer makes the component robust and free from inconsistent modification of the inner UI Element by the user.
Anyway, you admit yourself it is acceptable, so would you formally accept the answer?
Thank you.
--SA
Sergey Alexandrovich Kryukov 25-Jan-11 2:39am    
There is no such thing as miracle. Are you accepting my answer? Any further questions? problems?
Sergey Alexandrovich Kryukov 25-Jan-11 2:41am    
You see, User Control is pretty much an ad-hoc device. You cannot expect very universal solution with highly automated functionality.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900