Reputation: 1811
I could use some advice to set checkBox to isChecked = true when you select a row as a user. Have created a Listview, that has some GridViewColumns. One of these Columns has a checkBox inside. It look like this image:
The user can set the selected by clicking inside the CheckBox and thats okay. What I also want is that when the user select a row, like the blue one, the checkBox should be selected (Selected when blue, and unselected if you unselect it ofc)
In my Customer Model, I have one property which uses the INotifyPropertyChanged event.
// Room Selected property.
public bool Selected
{
get
{
return isChecked;
}
set
{
isChecked = value;
OnPropertyChanged("Selected");
}
}
I'm using this property to see what the user has selected when clicking further in my application.
Can you do something smart, so I still keep the property to the CustomerModel?
ListView code
<ListView Height="250" Width="Auto" ItemsSource="{Binding CustomerList}" Grid.Row="1" SelectionMode="Multiple" HorizontalAlignment="Center">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView >
<GridViewColumn Header="ID" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Id}" TextAlignment="Center" Width="40"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding FirstName}" Header="First name" Width="130"/>
<GridViewColumn DisplayMemberBinding="{Binding LastName}" Header="Last name" Width="130"/>
<GridViewColumn DisplayMemberBinding="{Binding PhoneNumber}" Header="Phone" Width="130"/>
<GridViewColumn DisplayMemberBinding="{Binding Email}" Header="Email" Width="250"/>
<GridViewColumn Header="Selected" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Selected}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Also the CustomerLits which is loaded inside the ListView is of type ObservableCollection.
Good day.
Update:
Have tried to create the start of a command.
SelectedChangeCommand = new RelayCommand(SelectedChangedCommand_DoWork, () => true);
// Selectionchanged Command
void SelectedChangedCommand_DoWork(object obj)
{
}
Now I'm not so sure what to do..
Upvotes: 0
Views: 857
Reputation: 1568
You can do it all in the XAML.
Set the IsChecked of the checkbox property binded to the listview selectedItem with a converter. If selectedItem is null return false otherwise return true.
You will end up with something like this:
<ListView Height="250" Name="myListView" Width="Auto" ItemsSource="{Binding CustomerList}" Grid.Row="1" SelectionMode="Multiple" HorizontalAlignment="Center">
....
<CheckBox IsChecked="{Binding ElementName=myListView, Path=SelectedItem, Converter={StaticResource MyConverter}"></CheckBox>
Give it a try :)
Upvotes: 1