我使用了XAML ListView,Listview中的一列是一个复选框。当数据绑定到ListView时,它会创建多行,并在列上打开一个复选框。我面临的问题是我希望用户只能选择一个复选框,而现在他可以选择多个。当用户选择一个复选框时,所有其他复选框都应该取消选中。
这是XAML部分。
<ListView ScrollViewer.VerticalScrollBarVisibility="Auto" Name="ClientList" Width="Auto" Height="Auto" BorderThickness="0.5" BorderBrush="#cccccc" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Stretch" Margin="20,10,60,0" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Focusable" Value="false"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Height" Value="30"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle1}">
<GridViewColumn Header="Client Name " x:Name="clienteName" DisplayMemberBinding="{Binding ClientName}" Width="390" />
<GridViewColumn Header="Client Code " x:Name="ClienteCode" DisplayMemberBinding="{Binding ClientCode}" Width="195" />
<GridViewColumn Header="Select" Width="57">
<GridViewColumn.CellTemplate>
<DataTemplate >
<Grid >
<CheckBox DataContext="{Binding ClientCode}" Click="CheckBox_Click" TouchDown="CheckBox_TouchDown"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
CS用于将数据绑定到ListView
List<Client> list = new List<Client>();
ClientList.Items.Clear();
list = getListFromDT(dt);
foreach (Client pr in list)
{
ClientList.Items.Add(pr);
}
答案 0 :(得分:0)
答案 1 :(得分:0)
void check_CheckedChanged(object sender, EventArgs e)
{
CheckBox sendercheck = sender as CheckBox;
if (sendercheck.Checked)
{
foreach (var c in Container.Children)
{
CheckBox check = c as CheckBox;
if (check != null)
{
if (check.Id != sendercheck.Id)
{
check.Checked = false;
}
}
}
}
}
这里的容器应该是你的清单