'How to allow check just single value ListView with checkboxes in WPF?
I write a wpf application , using MVVM. I have a ListView of products names with checkboxes (binding by ObservableCollection).
<ListView ItemsSource="{Binding productsOfProject}" Margin="10">
<ListView.View>
<GridView>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<!--Command="{Binding CheckCommand}"-->
<CheckBox IsChecked="{Binding IsChecked}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ProductCheck}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding productName}" Header="Product Name" />
</GridView>
</ListView.View>
</ListView>
Every product has an "IsChecked" property that updated according to the click.
I want to allow only one product to be marked. At each check, I call a function that marks the current checked value as unmarked, but still in the UI , the check is not cleared.
#region Event
public ICommand productCheck;
public ICommand ProductCheck
{
get
{
if (productCheck == null)
{
productCheck = new RelayCommand(p => productCheckFunc());
}
return productCheck;
}
}
private void productCheckFunc()
{
uncheckedProducts();
}
private void uncheckedProducts()
{
foreach (Product p in rvm.productsOfProject)
{
if (p.id != this.id)
p.IsChecked = false;
}
}
#endregion Event
How can I access to the List View and unchecked to current checked value ?
Solution 1:[1]
Issue solved by using Radio Button instead of Combo Box. In radio button selecting one value is the default, we will only need to set one group.
Solution :
<ListView ItemsSource="{Binding productsOfProject}" Margin="10">
<ListView.View>
<GridView>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<RadioButton GroupName="One" IsChecked="{Binding IsChecked}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ProductCheck}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding productName}" Header="Product Name" />
</GridView>
</ListView.View>
</ListView>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adi1992 |
