Radibuttons Ischecked属性在wpf mvvm中不起作用
我无法将两个单选按钮绑定到我的xaml表单和具有相同组名称的IsChecked属性,我也使用它们使用了nullabletoboolconverters。 然而,在我的代码中,radiobuttons的缺陷属性并没有改变(一旦我们在第二个单选符后碰到第一个单选按钮,它一点都没有触及断点),我分别绑定其中两个的缺省属性,因为我需要设置根据radiobuttons属性显示窗体上某些其他面板的可见性。
以下是我的xaml代码,后来是我的viewmodel.cs代码的单选按钮属性:
<RadiobuttonSelectedConverter x:Key="CheckedSelection"/>// declaring my converter class in my resource dictionary.
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="3" Margin="10,5,0,0">
<RadioButton GroupName="RadiosGroup" IsChecked="{Binding IsRadioButton1,Mode=TwoWay,
Converter={StaticResource CheckedSelection}, ConverterParameter=true}">
First</RadioButton>
<RadioButton GroupName="RadiosGroup"
Margin="40,0,0,0" IsChecked="{Binding IsRadioButton2,Mode=TwoWay,
Converter={StaticResource CheckedSelection}, ConverterParameter=true}">Second</RadioButton>
</StackPanel>
private bool _isRadioButton1;
public bool IsRadioButton1
{
get
{
return _isRadioButton1;
}
set
{
if _isRadioButton1!= value)
{
_isRadioButton1= value;
IsRadioButton2= false;
OnPropertyChanged("IsRadioButton1");
}
}
}
private bool _isRadioButton2;
public bool IsRadioButton2
{
get
{
return _isRadioButton2;
}
set
{
if (_isRadioButton2 != value)
{
_isRadioButton2 = value;
IsRadioButton1= false;
OnPropertyChanged("IsRadioButton2");
}
}
}
以下是我的转换器代码:
[ValueConversion(typeof(bool?), typeof(bool))]
public class RadiobuttonSelectedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool param = bool.Parse(parameter.ToString());
if (value == null)
{
return false;
}
else
{
return !((bool)value ^ param);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool param = bool.Parse(parameter.ToString());
return !((bool)value ^ param);
}
}
有人请提前帮我解决我的问题。
就我个人而言,我不会像这样编码相关的RadioButtons 。 由于您想要的选择行为与ListBox相同,我发现最简单的方法就是使用一个样式化的ListBox来为每个项目使用RadioButtons 。
代码隐藏通常会包含
ObservableCollection<string> Options string SelectedOption 我将在ListBox使用这种样式:
<Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2, 2, 2, 0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Transparent">
<RadioButton
Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
风格是这样应用的:
<ListBox ItemsSource="{Binding Options}"
SelectedValue="{Binding SelectedOption}"
Style="{StaticResource RadioButtonListBoxStyle}" />
你也可以使用别的东西而不是String作为集合,比如ChildViewModel ,然后根据当前的项目设置你的相关视图,这意味着你不必担心ViewModel关联面板的Visibility 。
<DockPanel>
<ListBox ItemsSource="{Binding OptionViewModels}"
SelectedValue="{Binding SelectedViewModel}"
Style="{StaticResource RadioButtonListBoxStyle}"
DockPanel.Dock="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ContentControl Content="{Binding SelectedViewModel}" />
</DockPanel>
但就你的实际问题而言,我可以想出3个可能表现不正确的原因。
首先是在周杰伦的回答概括:您设置IsChecked2在你的制定者为假IsChecked1 ,并IsChecked2设置IsChecked1在其二传手假的,所以最终的结果是这两个值都是假的。
第二是它可能是你的转换器的问题。 你说这是一个评论,它没有转换器正常工作,所以我认为这可能是问题的一部分。
末了,我相信改变分组RadioButtons会触发IsOptionA.IsSelected = false的老项目和IsOptionB.IsSelected = true新选择的项目,它可能这两个越来越某处交叉。
这里有几个问题。
bool? IsRadioButton1和IsRadioButton2属性bool? , TwoWay Binding就足够了,或者如果三态不适用于你,就把它保留为bool 。 RadioButton的值设置为false ,因此如果IsRadioButton1为true ,然后将IsRadioButton2设置为true ,则setter将调用IsRadioButton = false ,然后该设置器将调用IsRadioButton2 = false 。 他们最终都会成为false 。 if(value) IsRadioButton2 = false;你可能希望这样做if(value) IsRadioButton2 = false;
编辑
实际上,我记得, RadioButton并不意味着像CheckBox那样绑定到bool属性。 我认为你将所有的RadioButton绑定到一个属性,并使用Converter和ConverterParameter来设置属性。 我会在一分钟内找到一个例子并发布。
好的,这里有一个解决方案,使用派生的RadioButton类,它纯粹用于绑定:http://blogs.msdn.com/b/mthalman/archive/2008/09/04/wpf-data-binding-with-radiobutton。 ASPX
这里是一个相关的SO问题和答案:MVVM:绑定单选按钮到视图模型?
链接地址: http://www.djcxy.com/p/44665.html上一篇: Radiobuttons Ischecked property is not working in wpf mvvm
