TheSteelDuck
TheSteelDuck

Reputation: 181

Binding a value to a Radiobutton and Checkbox in a ListView

I have a ListView which have a column containing a radiobuttons and a column containing checkboxes. What I would like to do is bind this radiobutton to a value based on a int value on a property.

My ListView look like this:

   <ListView x:Name="lvSalesmen" ItemsSource="{Binding}" Margin="311,32,0,244" Grid.ColumnSpan="5">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Path=Id}"/>
                <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
                <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"/>
                <GridViewColumn Header="IsResponsible">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <RadioButton GroupName="IsResponsible" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="IsSecondary">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView> 

and this is where I set my DataContext in the code-behind file:

private void Vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
   lvSalesmen.DataContext = vm.Salesmen;
}

My ViewModel look like this:

    public class DistrictsListViewModel : INotifyPropertyChanged {
    
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    
        private List<Salesman> _salesman;
        private ISalesmenService _salesmenService = new SalesmenService();
    
        public DistrictsListViewModel()
        {
        }
    
        public async void GetAllSalesmenWithResponsibilityByDistrictId( int id)
        {
            Salesmen = await _salesmenService.GetSalesmanReponsibilityByDistrictIdAsync(id);
        }
    
        public List<Salesman> Salesmen {
            get { return _salesman; }
            set {
                _salesman = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Salesmen"));
            }
        }

Salesmen is a List where the Salesman object looks like this:

public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int IsResponsible { get; set; }
public int IsSecondary { get; set; }

What I would like to achieve is to check the radiobutton in the row where the 'IsResponsible'-property value equals 1. Only one radiobutton can be checked at a time. This I have achieved by adding the 'GroupName'. Furthermore, the checkbox should be checked in the 'IsSecondary'-column, if the IsSecondary-property value equals 1. Multiple checkboxes can be checked at the same time.

Upvotes: 0

Views: 153

Answers (1)

oleconer
oleconer

Reputation: 61

You have a general type mismatch problem. IsChecked is generally a bool value but there are only int values available.

There are two ways to work this out:

  1. You could change the type of IsResponsible and IsSecondary to bool in GetSalesmanReponsibilityByDistrictIdAsync
  2. You can add another property to bind to which internally converty the value of IsResponsible and IsSecondary to bool in the Getoperation

With either one of these suggestions you can then bind the given value to IsChecked in you WPF like:

<RadioButton IsChecked="{Binding IsResponsibleBool, Mode=OneWay}" GroupName="IsResponsible" />
<CheckBox IsChecked="{Binding IsResponsibleBool, Mode=OneWay}"/>

Possbile solution for suggestion #2:

public Boolean IsResponsibleBool 
{
   get => this.IsResponsbile == 1;      
}

public Boolean IsSecondaryBool 
{
   get => this.IsSecondary == 1;      
}

Upvotes: 1

Related Questions