radio button binding in WPF MVVM - wpf

can any one tell me how can we enable/ disable button by radio button in MVVM.

Generally, it does not require view model. You can bind properties of two elements directly by using NotConverter.
[ValueConversion(typeof(bool), typeof(bool))]
public class NotConverter : IValueConverter
{
public static readonly IValueConverter Instance = new NotConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool typedValue = (bool)value;
return !typedValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture);
}
}
< RadioButton Name=radio />
< Button IsEnabled={Binding Path=IsChecked, ElementName=radio, Converter={x:Static ns:NotConverter.Instance}} />

The ViewModel sample application of the WPF Application Framework (WAF) shows how to bind ViewModel properties to RadioButtons.

Related

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

How To Bind a Button inside a DataTemplate to a Window's Viewmodel Command?

I have a funny problem with Binding,I have a DataTemplate that contain a Button,and the DataTemplate had been placed in a Window(specView),i need to bind the Button.Command to a Commnad inside window's viewmodel,so i do this:
Command="{Binding DataContext.NewOfferNoCommand,ElementName=specView}"/>
but it doesn't get bound until i make a Converter that return the value:
public class ReturnValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
I really can't understand what the problem is?any idia?
You need to set the relative source in order for the binding to work.
{Binding DataContext.NewOfferNoCommand, RelativeSource={RelativeSource TemplatedParent}}

Binding to an array

I added a custom control library to my project and in that control there is an array DependencyProperty. Now when I try to bind that property in the client I get:
Tags of type 'PropertyArrayStart' are not supported in template sections.
The message is self explanatory but how do you set array properties in a DataTemplate?
You can use a IMultiValueConverter to build an array from multiple bindings. A converter is only required as you cannot use a MultiBinding without one.
public class MultipleValuesToArrayConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.ToArray();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

Silverlight binding - Apply Not or ! before binding to Checkbox.IsChecked property

I use to bind a bool variable of a class to a IsChecked property of Checkbox. What i want to do is to bind 'NOT' of the original value, like IsChecked = Binding Not(IsSelected), please let me know how this can be done.
Thanks
You need to use a Convertor. A convertor class implements IValueConverter and can convert the binding value into something else, in your case, negate it. You can do it like this:
public class BoolInverseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
return !(bool)value;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

WPF Binding Formatexception

In a C# Wpf application I have an XML Binded datasource.
I want to update some xml like this:
loop.Innerxml = "false"
This value is binded (as a boolean) to a control. However when doing this, a formatexception comes up saying that a string is not a valid boolean (logical). However if the false is not entered as a string, I can't update the innerxml...
Any advice?
You can use a Converter to convert your Strings to Booleans when the binding happens.
For more about converters, see http://www.scip.be/index.php?Page=ArticlesNET22&Lang=EN.
Code sample:
[ValueConversion(typeof(string), typeof(bool))]
public class StringToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return TypeDescriptor.GetConverter(typeof(bool)).ConvertFrom(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}

Resources