WPF Bind Textbox IsEnabled to Listbox SelectedItem

Using the MVVM style, I successfully bound ObservableCollection<string>to ListBox, showing values ​​as RadioButtons. Management behaves exactly as expected.

Now I have a problem with some TextBoxes related to this ListBox: I want when SelectedItemin ListBoxequal to a certain value (for example ValueForEnabled) TextBoxes to enable them otherwise they should be disabled.

I know that I need to bind to SeletedItem ListBox(with a name lbSource), but how is this done?

I want something like this (Pseudocode):

<TextBox  ...

    IsEnabled="{Binding ElementName=lbSource, Path=SelectedItem='ValueForEnabled',
                Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                ...            
/>
+3
source share
3

OK! (-) ! , :

<TextBox 
...
usual property definitions
...
                      >
                <TextBox.Style>
                    <Style>
                        <Setter Property="TextBox.IsEnabled" Value="False"/>
                        <Style.Triggers>                              
                            <DataTrigger Binding="{Binding ElementName=lbSource , Path=SelectedItem}" Value="ValueForEnabled">
                                <Setter  Property="TextBox.IsEnabled" Value="true"/>
                            </DataTrigger>                           
                        </Style.Triggers>                       
                    </Style>                   
                </TextBox.Style>
            </TextBox>
+6

, MVVM, ViewModel. , :

public class MyViewModel
{
  public ObservableCollection<string> myColl {get;set;}
  public string SelectedString {get;set;}

  public bool IsEnabled
  {
     get { return SelectedString == "Desired string value";}
  }
}

IsEnabled IsEnabled ViewModel

, , , , , , ( /​​ )

, -,

<TextBox IsEnabled={Binding IsEnabled} Text={Binding SelectedString}/>

,

+3

- ( ) bool, IsEnabledProperty...

, IValueConverter, :

public class StringToBoolConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return false;

        string keyword = value.ToString();
        if (keyword.Equals(parameter.ToString(), StringComparison.CurrentCultureIgnoreCase))
            return true;
        return false;
    }

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

    #endregion
}

Note how you do not need to implement the ConvertBack method? This is because you only need to turn strings into bools, and not vice versa ...

So you can declare an instance of your converter in xaml, for example

<Window
    ...
    xmlns:local="clr-namespace:WpfApplication1">

    <Window.Resources>
        <local:StringToBoolConverter x:Key="stringToBoolConverter" />
    </Window.Resources>

And finally, you can bind a TextBox to a ListBox SelectedValue, for example:

<TextBox Grid.Row="0" Width="90" Height="30" 
         IsEnabled="{Binding ElementName=lbSource, Path=SelectedValue, Converter={StaticResource stringToBoolConverter}, ConverterParameter=ValueForEnabled}">
</TextBox>

Note. This will only work if the ListBox contains strings, and you can be sure that the SelectedValue property is a string ...

+2
source

Source: https://habr.com/ru/post/1725699/


All Articles