locked
Binding Text to UserControl Property RRS feed

  • Question

  • Is there a way to bind data from a custom property in the usercontrol in XAML?

    I have a custom UserControl with a Label and a custom property "LabelString".  I have to databind that Label so that the user can tweak the LabelString property to change it.  (y:CustomUserControl LabelString="Test") would render the UserControl's label with the "Test" string.

    Monday, August 24, 2009 12:23 PM

Answers

  • Is there a way in XAML to inform the databinding that the binding is connected to itself (the custom UserControl) rather than the DataContext source?

    You could try using Element Binding. Here is a sample

    c#

        public partial class SilverlightControl3 : UserControl
        {
            public string StringValue
            {
                get { return (string)GetValue(StringValueProperty); }
                set { SetValue(StringValueProperty, value); }
            }
    
            public static readonly DependencyProperty StringValueProperty =
                DependencyProperty.Register("StringValue", typeof(string), typeof(SilverlightControl3), null);
    
            public SilverlightControl3()
            {
                InitializeComponent();
            }
    
            // this is for test.
            // when type word in textbox, StringValue would be changed,
            // then the textblock would be notified for updating value.
            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                StringValue = ((TextBox)sender).Text;
            }
        }
    xaml 
    <UserControl x:Class="DateUpDown_test.SilverlightControl3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="uc1" 
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel>
                <TextBox TextChanged="TextBox_TextChanged"/>
                <TextBlock Text="{Binding StringValue,ElementName=uc1}"/>
            </StackPanel>
        </Grid>
    </UserControl>

     

    Please have a try.

    Thanks,

    Thursday, August 27, 2009 5:32 AM

All replies

  •  LabelString would have to be a dependency property if you want to bind to it in xaml

     

    http://msdn.microsoft.com/en-us/library/cc221408%28VS.95%29.aspx

    Monday, August 24, 2009 12:33 PM
  • LabelString is a Dependency Property of the UserControl.  Is there a way in XAML to inform the databinding that the binding is connected to itself (the custom UserControl) rather than the DataContext source?

    Monday, August 24, 2009 1:10 PM
  • Is there a way in XAML to inform the databinding that the binding is connected to itself (the custom UserControl) rather than the DataContext source?

    You could try using Element Binding. Here is a sample

    c#

        public partial class SilverlightControl3 : UserControl
        {
            public string StringValue
            {
                get { return (string)GetValue(StringValueProperty); }
                set { SetValue(StringValueProperty, value); }
            }
    
            public static readonly DependencyProperty StringValueProperty =
                DependencyProperty.Register("StringValue", typeof(string), typeof(SilverlightControl3), null);
    
            public SilverlightControl3()
            {
                InitializeComponent();
            }
    
            // this is for test.
            // when type word in textbox, StringValue would be changed,
            // then the textblock would be notified for updating value.
            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                StringValue = ((TextBox)sender).Text;
            }
        }
    xaml 
    <UserControl x:Class="DateUpDown_test.SilverlightControl3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="uc1" 
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel>
                <TextBox TextChanged="TextBox_TextChanged"/>
                <TextBlock Text="{Binding StringValue,ElementName=uc1}"/>
            </StackPanel>
        </Grid>
    </UserControl>

     

    Please have a try.

    Thanks,

    Thursday, August 27, 2009 5:32 AM