none
Binding to properties inside a UserControl RRS feed

  • Question

  • I'm trying to bind to public properties in my user control. The code below builds without issue, however the slider value is not bound to the text in the user control. What am I missing?

    Test App:

    <Window x:Class="UserControlTest.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ucx="clr-namespace:UserControlX;assembly=UserControlX"
      Title="Window1" Height="100" Width="300">
      <Grid>
        <StackPanel>
        <Slider x:Name="mySlider" Minimum="0" Maximum="100" />
        <ucx:UserControl1 MyValue="{Binding ElementName=mySlider, Path=Value}" />
          </StackPanel>
      </Grid>
    </Window>
    
    
    namespace UserControlTest
    {
      /// <summary>
      /// Interaction logic for Window1.xaml
      /// </summary>
      public partial class Window1 : Window
      {
        public Window1()
        {
          InitializeComponent();
        }
      }
    }

     

    UserControl:

    <UserControl x:Class="UserControlX.UserControl1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="Auto" Width="Auto">
      <Grid>
        <TextBox Width="100" Text="{Binding Path=MyValue}" />
      </Grid>
    </UserControl>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace UserControlX
    {
      /// <summary>
      /// Interaction logic for UserControl1.xaml
      /// </summary>
      public partial class UserControl1 : UserControl
      {
        public UserControl1()
        {
          InitializeComponent();
        }
    
        public static readonly DependencyProperty MyValueProperty = DependencyProperty.Register("MyValue",
                                                     typeof(int),
                                                     typeof(UserControl1));
        public int MyValue
        {
          get
          {
            return (int)GetValue(MyValueProperty);
          }
          set
          {
            SetValue(MyValueProperty, value);
          }
        }
      }
    }
    

    Monday, July 19, 2010 4:16 PM

Answers

  • Found an answer on a another thread.

     

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cf53bf41-395c-4265-beb4-7f1fb83ab4be

    • Marked as answer by zeekial Monday, July 19, 2010 5:20 PM
    Monday, July 19, 2010 5:20 PM