Shamshiel
Shamshiel

Reputation: 2211

WPF - ComboBox SelectionChanged => Change TextBox Binding

I have a ComboBox and a TextBox. The TextBox is binding to a "default"-property in my ViewModel.

What I'm trying to accomplish is that when I change the Value in the ComboBox , that the property of the TextBox is changed to another property.

<ComboBox SelectedIndex="0" Name="ComboBox1">
    <ComboBoxItem>
        Messages1
    </ComboBoxItem>
    <ComboBoxItem>
        Messages2
    </ComboBoxItem>
</ComboBox>

 <TextBox Text="{Binding Messages1}" IsReadOnly="True" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Name="LogTextBox" />

I want to change the binding of the TextBox to Messages2. I tried many things but nothing seems to work.

Is there an easy solution?

Upvotes: 2

Views: 2651

Answers (2)

Bahman_Aries
Bahman_Aries

Reputation: 4808

Assuming you've implemented INotifyPropertyChanged you can do it like this:

Code behind:

        public string Message1
        {
            get { return (string)GetValue(Message1Property); }
            set { SetValue(Message1Property, value); }
        }

        // Using a DependencyProperty as the backing store for Message1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Message1Property =
            DependencyProperty.Register("Message1", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));

         public string Message2
        {
            get { return (string)GetValue(Message2Property); }
            set { SetValue(Message2Property, value); }
        }

        // Using a DependencyProperty as the backing store for Message2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Message2Property =
            DependencyProperty.Register("Message2", typeof(string), typeof(MainWindow), new PropertyMetadata(string.Empty));


        //an array of properties as combobox.Items
        public DependencyProperty[] AllowedProperties 
        { 
            get
            {
                return new DependencyProperty[] { Message1Property, Message2Property };
            }
        }

        //selected property as combobox.selectedItem
        DependencyProperty _chosenProperty;
        public DependencyProperty ChosenProperty
        {
            get
            {
                return _chosenProperty;
            }
            set
            {
                _chosenProperty = value;
                OnPropertyChanged("ChosenValue");
            }
        }

        //value of the selected property as textbox.text.
        public string ChosenValue
        {
            get
            {
                return ChosenProperty == null ? string.Empty : (string)GetValue(ChosenProperty);
            }
        }

XAML:

    <ComboBox ItemsSource="{Binding AllowedProperties}" 
              SelectedItem="{Binding ChosenProperty}"
              >
    </ComboBox>
    <TextBlock Text="{Binding ChosenValue}"/>

Upvotes: 2

Ben Cohen
Ben Cohen

Reputation: 1410

That was asked before - the best and most clean solution, yet not that generic - is creating few textboxes, that will be collapsed visible, besides the relevant one. When you update the combobox selected item, then update the visibility bindings of the textboxes.

Upvotes: 1

Related Questions