简体   繁体   中英

TextBox isEnabled binding dependant on combobox item

So i have a ComboBox bound to a list of items. { A,B,C,D,E }

<ComboBox x:Name="cmbDMS" ItemsSource="{Binding Types}" SelectedValue="{Binding Type,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="top" Width="120" SelectionChanged="cmbDMS_SelectionChanged" />

also a TextBox.

<TextBox IsEnabled="{Binding isTypeB}" Grid.Row="6" Width="250" Margin="0,2" />

I cannot figure out how to update the TextBox isEnabled property once the ComboBox SelectedValue has changed to B. It works once i exit and come back into the view but i want it to be instant.

Thanks.

I have done a sample for you to understand it. Since it's more of a view based thing, I suggest using a converter and include the code in the view and converter itself. See the code below. I tested it and it is working fine at my end.

When you select B the textbox is enabled, when selection changes to any other, the textbox is disabled.

XAML Code :

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <viewModel:TestConverter x:Key="TestConverter" />
    </Window.Resources>
    <Grid Margin="329,0,0,0">
        <ComboBox x:Name="cmbDMS" HorizontalAlignment="Left" VerticalAlignment="top" Width="120" >
            <ComboBoxItem Content="A"/>
            <ComboBoxItem Content="B"/>
            <ComboBoxItem Content="C"/>
            <ComboBoxItem Content="D"/>
            <ComboBoxItem Content="E"/>
        </ComboBox>
        <TextBox Grid.Row="6" Height="60" Width="250" Margin="0,2" IsEnabled="{Binding ElementName=cmbDMS, Path=Text, Converter={StaticResource TestConverter}}" />
    </Grid>
</Window>

TestConverter.cs Code :

using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfApplication1.ViewModel
{
    public class TestConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var si = value.ToString();
                if (si.Equals("B"))
                    return true;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

I have had some problems in the past with binding to SelectedValue and it properly raising events. Unless you have an explicit reason, I like to bind to the SelectedItem

The issue i have found with this operation, is that the binding to the bool object will not necessarily get updated via the changing of the SelectedItem of the ComboBox

If you would like the two to be linked, an easy way to do this is to raise the Property changed event for the bool isTypeB property within the setter of the "Type" propery:

(as i dont know the Type of "Type" i will assume it is a string):

public string Type
{
  get{...}
  set
  {
    //...set logic
    RaisePropertyChanged(); //will notify when "Type" changes
    RaisePropertyChanged("isTypeB"); //will notify that isTypeB is changed
  }
}
...
public bool isTypeB => Type == "TypeB";

Reference for RaisePropertyChanged :

    public event PropertyChangedEventHandler PropertyChanged;

    public virtual void RaisePropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

If your Binding works for the first time and later it doesn't, your notification about changing the property may not work. So make sure you binds to a property which is DependencyProperty or in your setter you raise PropertyChanged event (a member of INotifyPropertyChanged interface).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM