问题描述
我有一个“ SelectableItems”对象的名为“ CarEngineItems”的列表,该列表具有2个属性:ItemDescription和isSelected。
namespace DTOs
{
class CarEngines : INotifyPropertyChanged
{
public static List<SelectableItem> CarEngineItems { get; set; } = new List<SelectableItem>();
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}
}
}
namespace Models
{
public class SelectableItem : INotifyPropertyChanged
{
public string ItemDescription { get; set; }
public bool IsSelected { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}
}
}
我已经使用下面的用户控件为列表中的每个项目创建了一个单选按钮。
<UserControl.Resources>
<SelectableItem:SelectableItem x:Key="vm"></SelectableItem:SelectableItem>
<src:RadioButtonCheckedConverter x:Key="RadioButtonCheckedConverter" />
<CollectionViewSource
Source="{Binding Source={x:Static Application.Current},Path=ItemDescription}"
x:Key="ListingDataView" />
<DataTemplate x:Key="GroupingHeaderTemplate">
<TextBlock Text="{Binding Path=Name}" Style="{StaticResource GroupHeaderStyle}"/>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ItemsControl Name="RadioGroup" AutomationProperties.Name="RadioGroup" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton
Content="{Binding Path=ItemDescription}"
FlowDirection="RightToLeft"
IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" Style="{DynamicResource CustomradioButton}" Margin="20,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
<uc:CommonRadioButtonGroup x:Name="EnginesGroup"></uc:CommonRadioButtonGroup>
InitializeComponent();
EnginesGroup.RadioGroup.ItemsSource = DTOs.CarEngines.CarEngineItems;
在启动时添加了CarEngineItems。
CarEngines.CarEngineItems.Add(new SelectableItem
{
ItemDescription = "V8",IsSelected = Properties.Settings.Default.Engines.Equals("V8")
});
CarEngines.CarEngineItems.Add(new SelectableItem
{
ItemDescription = "Electric",IsSelected = Properties.Settings.Default.Engines.Equals("Electric")
});
Proper binding happens on startup.
但是在单击组中的下一个项目时,也会将其选中。
最终目的是选择一个复选框(动态创建),并在按下按钮(“保存”按钮)时仅将所选项目更新到属性表。但是在这种情况下,所有项目都在选择中。
解决方法
根据@o_w的评论,我正在更新答案。
我通过将单选按钮组绑定到SelectableItem的另一个属性来向单选按钮组添加组名。
<ItemsControl Name="RadioGroup" AutomationProperties.Name="RadioGroup" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton
GroupName="{Binding Path=ItemType}"
Content="{Binding Path=ItemDescription}"
FlowDirection="RightToLeft"
IsChecked="{Binding Path=IsSelected,Mode=TwoWay}"
Style="{DynamicResource CustomRadioButton}" Margin="20,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是更新的SelectableItem。
namespace Models
{
public class SelectableItem : INotifyPropertyChanged
{
public string ItemDescription { get; set; }
public bool IsSelected { get; set; }
public string ItemType { get; set; }//New
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}
}
}