我无法将两个radiobutton绑定到我的xaml表单和IsChecked属性,具有相同的组名,我也使用了nullabletoboolconverters.然而,我的代码中的radiobuttons缺血特性没有改变(一旦我们在第二个之后击中第一个放射性按钮,它根本没有达到断点)并且我正在绑定其中两个的缺血性质,因为我需要设置基于radiobuttons属性的表单上其他面板的可见性.
以下是我的xaml代码,后来是我的viewmodel.cs radiobutton属性代码:
<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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的转换器代码:
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
有人请提前帮助我解决我的问题..
就个人而言,我根本不会RadioButtons像这样编码.由于您想要的选择行为与a相同ListBox,我发现最简单地使用一个ListBox样式RadioButtons用于每个项目.
代码隐藏通常包含
ObservableCollection<string> Optionsstring 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>
Run Code Online (Sandbox Code Playgroud)
样式应用如下:
<ListBox ItemsSource="{Binding Options}"
SelectedValue="{Binding SelectedOption}"
Style="{StaticResource RadioButtonListBoxStyle}" />
Run Code Online (Sandbox Code Playgroud)
您还可以使用其他内容而不是a String作为集合,例如a ChildViewModel,然后根据当前项设置相关的View,这意味着您不必费心使用Visibility相关面板ViewModel.
<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>
Run Code Online (Sandbox Code Playgroud)
但至于你的实际问题,我可以想到它可能表现不正确的3个原因.
第一个是在Jay的答案中概述的:你IsChecked2在setter 中设置为false IsChecked1,并在其setter中IsChecked2设置IsChecked1为false,因此最终结果是两个值都为false.
第二个问题是转换器可能存在问题.你说这是一个没有转换器正常工作的评论,所以我认为这可能是问题的一部分.
最后,我认为更改分组RadioButtons将触发IsOptionA.IsSelected = false旧项目和IsOptionB.IsSelected = true新选择项目,并且可能这两者正在某处交叉.
| 归档时间: |
|
| 查看次数: |
8436 次 |
| 最近记录: |