I have a this code:
<ListView Height="238"
HorizontalAlignment="Left"
Name="listView1"
VerticalAlignment="Top"
Width="503"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding ID}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" />
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
That produce this window:
How do I know how many checkboxes are selected, and get the value Tag of each CheckBox that is selected?
i know it's old but for posterity if people stubble upon this here's the solution
<ListView Height="238"
HorizontalAlignment="Left"
Name="listView1"
VerticalAlignment="Top"
Width="503"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True"
SelectionChanged="listView1_SelectionChanged">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding ID}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" />
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
then in the cs file code this in the listView1_SelectionChanged
private List<MyObject> lstMyObject = new List<MyObject>();
private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (MyObject item in e.RemovedItems)
{
lstMyObject.Remove(item);
}
foreach (MyObject item in e.AddedItems)
{
lstMyObject.Add(item);
}
}
lstMyObject should be same type as your object binded to the list. and the code will simply add and remove reference to items of the original list to that list.
Now all you will have to do is loop through that list which will contain only the actually selected items. this works for single selection only except that the lstMyObject will just contain 1 record all the time.
It should be as simple as binding the IsChecked property of the CheckBox to a property on the ViewModel (you may need to add a new property if it doesn't already exist). Then, once the button is clicked, you would just iterate over all the items in the collection, and delete the ones that are checked (based on the value of the property on the ViewModel).
A suggestion...
Just like the Tag property in all Windows controls, I've always had a Tag property in all my data models for general purpose use at run-time. I use that property to hold checked state of the item in a ListView. In other circumstance, I use them to hold complex objects as well.
Related
I have a ListView that gets its values from a database, with the last column being buttons. What I am trying to do, is that when a button is clicked, the value of the Name in the same row will be returned.
Here's the relevant part of my XAML code:
<Grid Grid.ColumnSpan="2">
<ListView Name="PeopleList">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding [Name]}" />
<GridViewColumn Header="Phone" DisplayMemberBinding="{Binding [Phone]}" />
<GridViewColumn Header="Email" DisplayMemberBinding="{Binding [Email]}" />
<GridViewColumn Header="Actions" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content=">" Click="ActionButton_Click"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
You should be able to get the exact Button that was clicked from the sender argument of the Click event. From there you should be able to get Button.DataContext, which would hold a reference to the item that is being displayed on that row.
It would be something like:
private void ActionButton_Click(object sender, EventArgs args)
{
var rowItem = (sender as Button).DataContext as People;
string name = rowItem.Name;
}
I have this problem with my C# WPF app:
I have the checkboxes on every row but when I select (random) one or more checkboxes I noticed that other checkboxes on other rows (when I scroll to see the other rows) are selected, too. It's like there would be a function or something that checks automatically other checkbox different of those selected by me.
There is no C# code for the checkboxes (just the definitions of the functions for Checked, Unchecked). I use a query and ItemSource to fill the Datagrid.
So, is there a way to avoid the automatic selection of other checkboxes ?
Thankx
My XAML code:
<DataGridTemplateColumn x:Name="colonnaChiusoF" Header="Cf" Width="26">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=sc1, Mode=TwoWay}" Checked="checkedF" Unchecked="uncheckedF" Margin="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Actually it happens with other objects, too. For example, in this code the buttons and checkboxes are selected automatically random if I select/deselect some of them:
<ListView Name="listaView" ItemsSource="{Binding Path=Table}" Height="200" Width="400" ScrollViewer.VerticalScrollBarVisibility="Visible" IsSynchronizedWithCurrentItem="True" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
<ListView.View>
<GridView>
<GridViewColumn Header="Idprev" DisplayMemberBinding="{Binding Path=idPrev}" />
<GridViewColumn Header="DataPrev" DisplayMemberBinding="{Binding Path=dataPrev}" />
<!--<GridViewColumn Header="Test3" DisplayMemberBinding="{Binding Test3}" /> -->
<GridViewColumn Header="Chiudi">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0" BorderThickness="0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Button">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ToggleButton IsChecked="True">Button Text</ToggleButton>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The C# code:
DbDataAdapter da = daoObject.list_3(0, 0, arrayFilter);
DataSet dataset = new DataSet();
da.Fill(dataset);
listaView.DataContext = dataset.Tables[0].DefaultView;
Well, the problem should be the virtualization. If I UNCHECK "Enable row virtualization" it seems the checkboxes remain as I select them so there is no random automatically check-uncheck.
The problem now is that the records are loaded slower than with the virtualization enabled.
GridView with 5 columns:
- text
- radiobutton
- radiobutton
- radiobutton
- checkbox
The text column is automatically populated through a binding to a List<>.
About the radiobuttons I need they are mutually exclusive by row and not by column; their value is set by user interaction.
If I set the GroupName property like in the following exemple, all the radiobuttons are mutually exclusives both by row and by column.
Could you please advise? Thanks in advance!
Chris
<ListView Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="lstWords" VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Word" DisplayMemberBinding="{Binding Word}"/>
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Frequency" Width="60" DisplayMemberBinding="{Binding Frequency}" />
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Ins" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<RadioButton Name="radioToInsert" GroupName="radioWords" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Rep" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<RadioButton Name="radioToReplace" GroupName="radioWords" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Del" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<RadioButton Name="radioToDelete" GroupName="radioWords" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn HeaderContainerStyle="{StaticResource MyHeaderStyle}" Header="Inf" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkToInflect"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
If you can modify the object in the list that the ListView is binding to, then you can add a property in that object which uniquely identifies that particular object, just for this purpose. For example:
public class DataObject
{
public void DataObject()
{
Id = Guid.NewGuid();
}
public Guid Id{ get; private set; }
}
Then you can bind your GroupName to this property:
<RadioButton Name="radioToInsert" GroupName="{Binding Id, Mode=OneWay}" />
Because each row represents a different object in your list, so this Id is only shared in the scope of 1 row.
Something like this:
public class EffectViewModel
{
public string Name ...
ObservableCollection<KeyValuePair<int,object>> settings
public ObservableCollection<KeyValuePair<int,object>> Settings
{
get {return this.settings;}
set
{
this.settings = value;
this.RaisePropertyChanged ( "Settings" );
}
}
}
Right now I am trying to bind it like this:
EffectWindowViewModel.Effects is of type ObservableCollection<EffectViewModel>.
<ListView Width="1000"
Height="600"
ItemsSource="{Binding EffectWindowViewModel.Effects}">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Key}"
Header="Name" />
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Value}"
Header="Value" />
</GridView>
</ListView.View>
</ListView>
But I don't know how to specify .Settings property.
Any ideas?
your actual binding will not work cause EffectViewModel has no key and Value Property. i really dont know what your listview should display. if you want a list of EffectViewModels then the Itemssource is right. if you want further for each EffectViewModel to display the settings. then you need somekind of itemsscontrol with Itemssource={Binding Settings}. this itemsscontrol of course will need a itemsstemplate with your Key and Value.
i have no VS here atm, but your GridViewColumn needs a kind of CellTemplate. and this template should consist of a itemscontrol. because you have 2collections!
this code is probably not right but should take you in the right direction
<ListView Width="1000"
Height="600"
ItemsSource="{Binding EffectWindowViewModel.Effects}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Settings}">
<GridViewColumn.CellTemplate>
<DataTemplatex:Key="myCell4Settings">
<ListView ItemsSource="{Binding.}">
<ListView.View>
<GridView>
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Key}"
Header="Name" />
<GridViewColumn Width="Auto"
DisplayMemberBinding="{Binding Value}"
Header="Value" />
</GridView>
</ListView.View>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView>
</DataTemplate>
</GridView>
</ListView.View>
btw you could also use 2 lists independent. one parent Combobox or listbox (x:Name=parent) with itemssource=EffectWindowViewModel.Effects and a second ListView like you have, with the itemssource binding:
ItemsSource="{Binding ElementName=parent, Path=SelectedItem.Settings}"
You could try:
ItemsSource="{Binding ElementName=Settings, Path=EffectWindowViewModel.Effects}"
How to display treeview inside a list view, actually this listview has headers namely workbook, description,date, so when I display the tree view inside the listview, the treeview should open as per the listview headers.
My question is :
1. How to insert treeview inside Listview and my xaml code is
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListView Name="lstViewWorkbooks" HorizontalAlignment="Left" >
<ListView.View >
<GridView >
<GridViewColumn Header="WorkBook" Width="200" DisplayMemberBinding="{Binding AnyWorkbook}" />
<GridViewColumn Header="Description" Width="660" DisplayMemberBinding="{Binding DescName}" />
<GridViewColumn Header="Date" Width="183" DisplayMemberBinding="{Binding WorkbookDate}" />
</GridView>
</ListView.View>
<TreeView HorizontalAlignment="Left" Margin="195,76,0,-76" Name="tviewPojectView" Width="120" />
</ListView>
</ScrollViewer>
I am using MVP architecture, with C# and wpf.
The treeview in the above xaml code is enough to display the data?
Please help me
Thanks
Ramm
If you want the tree view inside a cell in your grid, you need to place it inside the cell template for the correct column like this:
<GridViewColumn Header="WorkBook" Width="200" DisplayMemberBinding="{Binding AnyWorkbook}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TreeView HorizontalAlignment="Left" Margin="195,76,0,-76" Name="tviewPojectView" Width="120" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>