WPF Bind IsEnabled to Listbox SelectedItem
Solution 1
OK! Solved it (in another way) myself! For anyone who wants to know:
<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>
Solution 2
Personally I think that if you're using MVVM then you should have the code remain in your ViewModel. so if your VM is like this:
public class MyViewModel
{
public ObservableCollection<string> myColl {get;set;}
public string SelectedString {get;set;}
public bool IsEnabled
{
get { return SelectedString == "Desired string value";}
}
}
You then would just bind the textboxes IsEnabled property to your IsEnabled property on your ViewModel
The reason I say this is your requirements may change as to when to have the textboxes enabled and if you do it this way you don't have to touch your view(where code/logic should not reside)
So now you do this in your view and thats it
<TextBox IsEnabled={Binding IsEnabled} Text={Binding SelectedString}/>
Hope I understand your problem and that this helps
Solution 3
One way to skin this cat would be by converting the string (in the listbox) into a bool to pass into the IsEnabledProperty...
First, create a class that implements the IValueConverter interface, like:
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
}
Notice how you don't need to implement the ConvertBack method? That's because you only need to turn strings into bools, not vice-versa...
So you can declare an instance of your converter in the xaml, like
<Window
...
xmlns:local="clr-namespace:WpfApplication1">
<Window.Resources>
<local:StringToBoolConverter x:Key="stringToBoolConverter" />
</Window.Resources>
And finally, you can bind the TextBox to the ListBox's SelectedValue, like:
<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...
Savvas Sopiadis
Updated on June 04, 2022Comments
-
Savvas Sopiadis almost 2 years
Using MVVM style I have successfully bound an
ObservableCollection<string>
to aListBox
, showing up the values asRadioButton
s. The control behaves exactly as expected.Now I have an issue regarding some
TextBox
es bound to thisListBox
: I want whenever theSelectedItem
in theListBox
is equal to a specific value (e.g.ValueForEnabled
) theTextBox
es to be enabled otherwise they should be disabled.I know I have to bind to
SeletedItem
of theListBox
(namedlbSource
) but how exactly is this done?I want something like this (Pseudo code):
<TextBox ... IsEnabled="{Binding ElementName=lbSource, Path=SelectedItem='ValueForEnabled', Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ... />