Alter height when checkbox selected? - wpf

I'm trying to bind element's Height value to Checkbox.IsChecked property. Why that's not working?
<Window.Resources>
<local:BoolToHeightConverter x:Key="BoolToHeightConverter"/>
</Window.Resources>
<Button Name="JustBtn" Content="Hello World"/>
<CheckBox IsChecked="{Binding ElementName=JustButton, Path=Height, Converter=BoolToHeightConverter}" />
[ValueConversion(typeof(Nullable<bool>), typeof(double))]
public class BoolToHeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return double.NaN;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
It doesn't even initalize the window. Says:
'IValueConverter' type does not have a public TypeConverter class

There are a couple of problems. First, it looks like you are trying to modify the Height property when the CheckBox is checked. If this is the case, you should implement your logic in the ConvertBack method of the converter, and specify a Mode on the Binding. Secondly, your Binding should use a StaticResource to reference your converter:
<CheckBox IsChecked="{Binding ElementName=JustButton, Path=Height, Converter={StaticResource BoolToHeightConverter}, Mode=OneWayToSource}" />

I'm sorry - my bad: I forgot to attach converter through StaticResource.
Sorry guys...

Related

CheckBox Binding with Viewmodel property and IConverter not working

I have a simple CheckBox and I want to bind a value from my ViewModel to the IsChecked property of the CheckBox. The value derived from ViewModel is of type sbyte.
I have included a small converter class for that.
However, this code is not working.
Please suggest the right way of doing this.
XAML
<UserControl>
<UserControl.Resources>
<local:TrueFalseConverter x:Key="TFC"/>
</UserControl.Resources>
<CheckBox x:Name="chkOutOfSales" DataContext="{Binding DCIM}" IsChecked="{Binding CurrentRec.Out_of_Sales, Converter={StaticResource TFC},Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</UserControl>
Code Begind converter:
public class TrueFalseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)value == true) ? 1 : 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((sbyte)value == 1) ? true : false;
}
}
I found the solution...I had mistyped Out_Of_Sales as Out_of_Sales in my viewmodel...that was the issue...:-).
Thanks for your time..

WPF - binding complex objects to simple controls

I have created a class named "BoundProperty" which contains a property "Value".
Binding to a property which is an instance of that class looks this way
(Age is a BoundProperty):
<TextBox Text="{Binding MyModel.Age.Value, Mode=TwoWay}" />
Is there a way to make the binding look this way and additionally still retain it two-way?
<TextBox Text="{Binding MyModel.Age, Mode=TwoWay}" />
I can't use implicit/explicit conversion operators, for this "BoundProperty" initialization requires special parameters, which need to be copied from original object.
Thanks,
AD
If Value is public you can use ValueConverter:
public class BoundPropertyConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var age = value as BoundProperty;
if (age == null)
return string.Empty;
return age.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int age;
if (int.TryParse(value.ToString(), out age))
return new BoundProperty() {Value = age};
return null;
}
}
Then in xaml define namespace to this converter
xmlns:converters="clr-namespace:Your.Namespace"
And then in Resources area write something like this:
<converters:BoundPropertyConverter x:Key="BoundPropertyConverter"/>
And last but not least:
<TextBox Text="{Binding MyModel.Age, Mode=TwoWay, Converter={StaticResource BoundPropertyConverter}" />

How can i change the backround color of a rectange by clicking on a checkbox

Im Using Blend and have the following Problem:
How can i Change the Background Color of a rectangle by clicking on a checkbox? When i go via right click, edit template and then edit a copy i can only edit the current object. In this cas it would be the checkbox. But i want to use the checkbox to edit the style of a rectange object. Is this possible?
you can bind to the IsChecked property and use a ValueConverter
<CheckBox x:Name="cb" />
<Rectangle Fill="{Binding ElementName=cb, Path=IsChecked, Mode=OneWay, Converter={StaticResource CheckedToBackgroundConverter}}" />
the value converter
public class CheckedToBackgroundConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is bool && (bool)value ? Brushes.Blue : Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
hope that helps

DataContextChanged event of CheckBox

I am working in Silverlight4 VS 2010.I have placed a CheckBox in my .xaml file. I need it's DataContextChanged event. But unfortunately i didn't find it.
Here is my CheckBox :
<CheckBox x:Name="chkRegion" Content="{Binding name}" Click="CheckBox_Click" ></CheckBox>
Could you please help me in to find DataContextChanged in SL 4 VS 2010.
Thanks, Rajbir
Implement an Converter (its just a simple class which gets derived from IValueConverter and implement the interface methods)
public class ChangeIsCheckedValConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (value != null)
{
//value here is the object which you are binding to the DataContext of
Checkbox ; //return the bool (true or false) based on your valued
binded to your checkbox
}
public object ConvertBack(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
return null;
}
You will have to add the name space of you newly implemnted converter whereever you will want to use .
Then use this converter where your checkbox is defined in datatemplate as below :
//First define the key as below :
<converters:VisibilityConverter x:Key="changeConverter" />
<CheckBox x:Name="chkRegion" Content="{Binding name}" IsChecked={Binding ,Converter={StaticResource changeConverter}}"} Click="CheckBox_Click" ></CheckBox>

how to bind a boolean to combobox in wpf

Well I was wondering how to bind a boolean property to a combobox.Combobox will be a yes/no combobox.
You could use a ValueConverter to convert the boolean value to a ComboBox index and back. Like this:
public class BoolToIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)value == true) ? 0 : 1;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((int)value == 0) ? true : false;
}
}
}
Assuming Yes is on index 0 and No on index 1. Then you'd have to use that converter in binding to the SelectedIndex property. For this, you declare your converter in your resources section:
<Window.Resources>
<local:BoolToIndexConverter x:Key="boolToIndexConverter" />
</Window.Resources>
Then you use it in your binding:
<ComboBox SelectedIndex="{Binding YourBooleanProperty, Converter={StaticResource boolToIndexConverter}}"/>
I have found myself using the IsSelected property of the ComboBox items for this in the past. This method is entirely in xaml.
<ComboBox>
<ComboBoxItem Content="No" />
<ComboBoxItem Content="Yes" IsSelected="{Binding YourBooleanProperty, Mode=OneWayToSource}" />
</ComboBox>
First solution is to replace your 'Yes/No' combobox with a checkbox because, well, checkbox exists for a reason.
Second solution is to fill your combobox with true and false objects and then bind the 'SelectedItem' of your combobox to your Boolean property.
Here is an example (replace enabled/disabled with yes/no):
<ComboBox SelectedValue="{Binding IsEnabled}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={x:Static converters:EnabledDisabledToBooleanConverter.Instance}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.Items>
<system:Boolean>True</system:Boolean>
<system:Boolean>False</system:Boolean>
</ComboBox.Items>
</ComboBox>
Here is Converter:
public class EnabledDisabledToBooleanConverter : IValueConverter
{
private const string EnabledText = "Enabled";
private const string DisabledText = "Disabled";
public static readonly EnabledDisabledToBooleanConverter Instance = new EnabledDisabledToBooleanConverter();
private EnabledDisabledToBooleanConverter()
{
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Equals(true, value)
? EnabledText
: DisabledText;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//Actually won't be used, but in case you need that
return Equals(value, EnabledText);
}
}
And no need to play with indices.

Resources