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..
Related
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>
I Silverlight5 with mvvm pattern i have one doubt.
In xaml i have used one textblock and i bind some id in it.
If the textblock content value is 1 or 2 means
yet another textbox is visible or else that is collapsed.. how to acheive that..
my code:
<TextBlock Name="textBlock1" Text="{Binding id}" Loaded="textBlock1_Loaded" Visibility="Collapsed" />
<TextBox Text="{Binding name,Mode=TwoWay}" x:Name="t1" Visibility="{Binding IsVisible,Converter={StaticResource visibilityconverter}}" />
in view model i had created the property for id and raised the event and bind the value to textblock.
to convert the value to visible i have a visibilityconverter class in one separate folder named "Converters"
public class visibilityconverter:IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (id==1 && id==2)
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
in the above visibleconverter class how i cna get the id value from viewmodel and check it..
If i got the value from viewmodel to visibilityconverter means i will proceed further.
tell me if u can..!
Hi i have found the solution..
In xaml give the following:
<TextBox Text="{Binding name,Mode=TwoWay}" x:Name="t1" Visibility="{Binding id,Converter={StaticResource visibilityconverter}}" />
In visibilityConverter class:
public class visibilityconverter:IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
string v = value.ToString();
if (v =="1" || v=="2")
{
return Visibility.Visible;
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Problem Solved... !
I'm using a ListBox in WPF, and I want to enable/disable a button only when an item from the ListBox is actually selected.
The problem is that my button does an action that retrieves the name of the item, and since at initialisation, no item is selected (and I want to keep it that way), I'm getting an error because I'm performing logic on a null object ...
I really looked around and I couldn't find one =/
Have a nice day =)
make a value converter and bind the buttons IsEnabled to the SelectedIndex of the Listbox using the converter.
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int ndx = (int)value;
if ( ndx < 0 ) return false;
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
<Window.Resources>
<my:MyConverter x:Name="MyConverter"/>
</Window.Resources>
<ListBox x:Name="MyListBox"></ListBox>
<Button IsEnabled="{Binding Path=SelectedIndex, ElementName=MyListBox, Converter={StaticResource MyConverter}}"/>
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.
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...