我有一个使用MVVMLight的WPF应用程序。
我正在尝试在我的UI数据输入表单中绑定一个单选按钮。下面的XAML用于绑定到单选按钮,当我编辑并首次显示表单时,选中正确的单选按钮。但是当我去保存更改时,MySelectedItem.ItemID不会更新。
如何让选定的单选按钮更新MySelectedItem?
//View
<StackPanel
Grid.Row="3"
Grid.Column="1"
Orientation="Horizontal"
Margin="2,5,2,24" Grid.RowSpan="2">
<RadioButton
GroupName="MyGroup"
Margin="4"
IsChecked="{Binding Path=MySelectedItem.ItemID,
Mode=TwoWay,
Converter={StaticResource App_RadioBoolToIntConverter},
ConverterParameter=2}"
Content="Choice 1"/>
<RadioButton
GroupName="MyGroup"
Margin="4"
IsChecked="{Binding Path=MySelectedItem.ItemID,
Mode=TwoWay,
Converter={StaticResource App_RadioBoolToIntConverter},
ConverterParameter=1}"
Content="Choice 2"/>
</StackPanel>
//View Model
public const string MySelectedItemPropertyName = "MySelectedItem";
private MyModelObject _MySelectedItem = null;
/// <summary>
/// Sets and gets the MySelectedItem property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public MyModelObject MySelectedItem
{
get
{
return _MySelectedItem;
}
set
{
if (_MySelectedItem == value)
{
return;
}
RaisePropertyChanging(MySelectedItemPropertyName);
_MySelectedItem = value;
RaisePropertyChanged(MySelectedItemPropertyName);
}
}
//Converter App_RadioBoolToIntConverter
public class RadioBoolToIntConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int integer = (int)value;
if (integer == int.Parse(parameter.ToString()))
return true;
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return parameter;
}
}