none
User Control Data Binding RRS feed

  • Question

  • I have a user control (Time Picker) which creates 3 textboxes (HH, MM, AM/PM)

    Now on  my main window, I can display this as a user control and everything works fine.

    What I need is another textbox in the main window or a way to multibind a textbox to combine the three textboxes to show HH:MM AM as a single resource for time checking.

    How would I go about this?

    Thanks

    Tuesday, September 23, 2014 1:07 PM

Answers

  • You could bind the Text property of the TextBoxes in the UC to some source properties and then bind to these same source properties and using a MultiConverter in the window:

    UC:

    <UserControl x:Class="WpfApplication2.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
      <StackPanel>
        <TextBox Text="{Binding Hh}"/>
        <TextBox Text="{Binding Mm}"/>
        <TextBox Text="{Binding Ss}"/>
      </StackPanel>
    </UserControl>
      public partial class UserControl1 : UserControl
      {
        public UserControl1() {
          InitializeComponent();
          this.DataContext = this;
          this.Hh = DateTime.Now.Hour;
          this.Mm = DateTime.Now.Minute;
          this.Ss = DateTime.Now.Second;
        }
    
        public int Hh {
          get;
          set;
        }
    
        public int Mm {
          get;
          set;
        }
    
        public int Ss {
          get;
          set;
        }
      }


    Window:

        <local:UserControl1 x:Name="uc"/>
    
        <TextBox>
          <TextBox.Resources>
            <local:TheConverter x:Key="conv"/>
          </TextBox.Resources>
          <TextBox.Text>
            <MultiBinding Converter="{StaticResource conv}">
              <Binding Path="Mm" ElementName="uc"/>
              <Binding Path="Hh" ElementName="uc"/>
              <Binding Path="Ss" ElementName="uc"/>
            </MultiBinding>
          </TextBox.Text>
          </TextBox>
    


    Converter class:

      public class TheConverter : IMultiValueConverter
      {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
          int hh = (int)values[0];
          int mm = (int)values[1];
          int ss = (int)values[2];
    
          return string.Format("{0}/{1}/{2}", hh, mm, ss);
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
          throw new NotImplementedException();
        }
      }
    

    Please remember to close your threads by marking helpful posts as answer by clicking on the "Mark as answer" link below the helpful post.

    Tuesday, September 23, 2014 1:20 PM

All replies

  • You could bind the Text property of the TextBoxes in the UC to some source properties and then bind to these same source properties and using a MultiConverter in the window:

    UC:

    <UserControl x:Class="WpfApplication2.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
      <StackPanel>
        <TextBox Text="{Binding Hh}"/>
        <TextBox Text="{Binding Mm}"/>
        <TextBox Text="{Binding Ss}"/>
      </StackPanel>
    </UserControl>
      public partial class UserControl1 : UserControl
      {
        public UserControl1() {
          InitializeComponent();
          this.DataContext = this;
          this.Hh = DateTime.Now.Hour;
          this.Mm = DateTime.Now.Minute;
          this.Ss = DateTime.Now.Second;
        }
    
        public int Hh {
          get;
          set;
        }
    
        public int Mm {
          get;
          set;
        }
    
        public int Ss {
          get;
          set;
        }
      }


    Window:

        <local:UserControl1 x:Name="uc"/>
    
        <TextBox>
          <TextBox.Resources>
            <local:TheConverter x:Key="conv"/>
          </TextBox.Resources>
          <TextBox.Text>
            <MultiBinding Converter="{StaticResource conv}">
              <Binding Path="Mm" ElementName="uc"/>
              <Binding Path="Hh" ElementName="uc"/>
              <Binding Path="Ss" ElementName="uc"/>
            </MultiBinding>
          </TextBox.Text>
          </TextBox>
    


    Converter class:

      public class TheConverter : IMultiValueConverter
      {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
          int hh = (int)values[0];
          int mm = (int)values[1];
          int ss = (int)values[2];
    
          return string.Format("{0}/{1}/{2}", hh, mm, ss);
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
          throw new NotImplementedException();
        }
      }
    

    Please remember to close your threads by marking helpful posts as answer by clicking on the "Mark as answer" link below the helpful post.

    Tuesday, September 23, 2014 1:20 PM
  • Thank you Magnus,

    It works fine in my test environment.
    I will try it in my app tomorrow.

    Regards
    Quentin

    Tuesday, September 23, 2014 1:54 PM