Reputation: 8223
Using MVVM style I have successfully bound an ObservableCollection<string>
to a ListBox
, showing up the values as RadioButton
s. The control behaves exactly as expected.
Now I have an issue regarding some TextBox
es bound to this ListBox
: I want whenever the SelectedItem
in the ListBox
is equal to a specific value (e.g. ValueForEnabled
) the TextBox
es to be enabled otherwise they should be disabled.
I know I have to bind to SeletedItem
of the ListBox
(named lbSource
) 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}"
...
/>
Upvotes: 4
Views: 11370
Reputation: 12998
Here's my version of the answer above. In my case I wanted to disable an entire GroupBox
of controls if there was no selection.
The key thing in this case was to use {x:Null}
to detect the case where nothing is selected. So the style defaults IsEnabled
to True
and the trigger sets it to False
when SelectedItem
is null
.
<GroupBox.Style>
<Style>
<Setter
Property="GroupBox.IsEnabled"
Value="True"
/>
<Style.Triggers>
<DataTrigger
Binding="{Binding ElementName=GroupList,
Path=SelectedItem}"
Value="{x:Null}"
>
<Setter
Property="GroupBox.IsEnabled"
Value="False"
/>
</DataTrigger>
</Style.Triggers>
</Style>
</GroupBox.Style>
Upvotes: 0
Reputation: 11091
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
Upvotes: 3
Reputation: 8223
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>
Upvotes: 7
Reputation: 7709
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...
Upvotes: 2