- ( ) 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 ...
source
share