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.
Related
I have a ListView that is going to show some values of a Google calendar event, the application let you see what events do you have for one specific date, and you can add more, edit, or delete the events.
By the moment, i am showing all the events just for testing, and i am using a gridview for the properties to reorder and the headers, but the event have too much values and is too wide, what i want is split this columns in two rows
Column A, Column B, Column C
info info info
Column D, Column E...
info info
for each element.
Instead of
Column A, B, C...
info info info info
in one row.
is there any way to do this with gridview? or i have to resign myself and do something similar with custom controls or Grids.
My listview
<ListView Background="Transparent"
x:Name="DatosEvento"
Margin="5"
MinWidth="{Binding ActualWidth, ElementName=Calendar}"
Width="Auto"
ItemsSource="{Binding Path=Eventos}"
Grid.Row="1"
Grid.Column="1">
<ListView.View>
<GridView>
<GridViewColumn Header="Organizador" DisplayMemberBinding="{Binding Path=Organizer.DisplayName}" />
<GridViewColumn Header="Correo Organizador">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Organizer.Email}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Nombre Evento" DisplayMemberBinding="{Binding Path=Summary}"/>
<GridViewColumn Header="Estado" DisplayMemberBinding="{Binding Path=Status}"/>
<GridViewColumn Header="Fecha Fin" DisplayMemberBinding="{Binding Path=End.Date}"/>
<GridViewColumn Header="Actualizado el" DisplayMemberBinding="{Binding Path=Updated.Date}"/>
<GridViewColumn Header="Enlace">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=HtmlLink}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
as i cant mark a comment as the solution, i have change the gridview for a mix of wrap and stackpanels, as #BionicCode suggested (he suggested the wrappanel i just added some stackpanels to do something similar to the gridview)
Hello Can anyone tell where did I made mistake in code below,
<ListView x:Name="ServiceListView"
Loaded="ServiceListView_Loaded"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
VirtualizingPanel.IsVirtualizing="True"
GridViewColumnHeader.Click="ServiceListSort"
ScrollViewer.IsDeferredScrollingEnabled="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<ListView.View>
<GridView>
<GridViewColumn Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<RadioButton IsChecked="{Binding Status}" x:Name="StartService" Checked="StartService_Checked" GroupName="{Binding ServiceName}" Style="{DynamicResource StartServiceStyle}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="50" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<DataTemplate.Resources>
<local:InvertBooleanConverter x:Key="InvertBooleanConverter" />
</DataTemplate.Resources>
<RadioButton IsChecked="{Binding Status, Converter={StaticResource InvertBooleanConverter}}" Checked="StopService_Checked" x:Name="StopService" GroupName="{Binding ServiceName}" Style="{DynamicResource StopServiceStyle}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="120" DisplayMemberBinding="{Binding ReccomendedStatus}">
<GridViewColumn.Header>
<GridViewColumnHeader Content="Recommendation"/>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="750" DisplayMemberBinding="{Binding ServiceName}">
<GridViewColumn.Header>
<GridViewColumnHeader Content="Description"/>
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Currently it has 20-40 items which are visible only 7, it takes around 5-7 seconds, removing both radiobutton Checked event time reduces to 2-4 seconds
private void ServiceListView_Loaded(object sender, RoutedEventArgs e)
{
ServiceListView.ItemsSource = DATA_CONTROLS.ServiceData.Values;
}
public static Dictionary<int, DataObject> ServiceData = new Dictionary<int, DataObject>();
Why it takes so long to show in msdn I saw that showing 1000 items only takes 1.6 second, why this method which contains 20-40 items takes 5-7 seconds? I really need to know what causing such performance issue. Thank you.
Found a issue: While I setting: ServiceListView.ItemsSource = DATA_CONTROLS.ServiceData.Values; It automaticly fires radiobutton events which would cause such performance drop, also usage IsAsynch improved also performance.
Add 'IsAsync=True' in your ListView,to increase a performance
I am designing a GridView which is nested in a ListView. All things are fine but I want to apply some style on Row of GridView but cannot find predefined GridViewRow tag in xaml.
Actually I want to show lining at the borders of each row to show it separate.
<ListView x:Name="gridTopics" BorderThickness="2" ItemsSource="{Binding Path=TOPICSINFO}" HorizontalAlignment="Left" Margin="91,79,0,0" VerticalAlignment="Top" Height="242" Width="310" BorderBrush="#FF32A3D6">
<ListView.Effect>
<DropShadowEffect Color="#FF9ADDFB" />
</ListView.Effect>
<ListView.View>
<GridView x:Name="GridViewControl">
<GridView.Columns>
<GridViewColumn x:Name="colPageNo" DisplayMemberBinding="{Binding PAGENO}" Width="50" Header="Pages" />
<GridViewColumn x:Name="colListTopics" Width="241" Header="Suggested Topics">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="colTopics" ItemsSource="{Binding LISTTOPICS}"
SelectedIndex="0"
/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
You want to use a GridPanel to do this, it will allow you define and style rows and columns.
http://wpftutorial.net/GridLayout.html
i'm having a few problems working with a gridview and a combobox inside of it.
Here is the code for my ListView control:
<ListView Height="139" HorizontalAlignment="Left" Margin="10,158,0,0" Name="lvAppointment" VerticalAlignment="Top" Width="250" MinWidth="350">
<ListView.View>
<GridView>
<GridViewColumn Header="Appointment" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=Appointment}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Type" Width="170">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ???/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Done" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Done}" IsThreeState="False"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
I'm popluating the list from a sql ce database via c# with the following code
using (SqlCeCommand sqlCeAppointment = new SqlCeCommand("SELECT appid,appointment,done,apptype.type FROM appointment INNER JOIN apptype ON appointment.refatid = apptype.atid WHERE refeventid = #eventid;", sqlCeConn))
{
sqlCeAppointment.Parameters.AddWithValue("#eventid", ((cListEventItem)lvEvent.SelectedItems[0]).id);
using (SqlCeDataReader sqlCeAppointmentReader = sqlCeAppointment.ExecuteReader())
{
lvAppointment.Items.Clear();
while (sqlCeAppointmentReader.Read())
{
lvAppointment.Items.Add(new cListAppointmentItem { id = sqlCeAppointmentReader.GetGuid(sqlCeTerminReader.GetOrdinal("appid")), Appointment = sqlCeAppointmentReader.GetDateTime(sqlCeTerminReader.GetOrdinal("appointment")), Type = sqlCeAppointmentReader.GetString(sqlCeTerminReader.GetOrdinal("type")), Done = sqlCeAppointmentReader.GetByte(sqlCeTerminReader.GetOrdinal("done")) });
}
}
}
I can popluate the list just fine. But i want "Type" to be a combobox so the user can select the apropriate type of the appointment (its a list of appointments connected to an event). This combobox should be filled with data thats inside a table of the sql ce database (apptype). This table is not static, the users can add and delete items from this list.
I have tried a few ways i found via google, but failed. I guess i'm having problems understanding how this works/should work.
I hope someone can help me :(
Thanks in advance
I would suggest getting a list of appointment-types on the MainViewModel. Then bind the ItemsSource of the ComboBox to that List and bind the SelectedItem to the AppointmentItem type.
<UserControl x:Name="TheUserControl">
<ListView>
<GridView>
<GridViewColumn Header="Type" Width="170">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ElementName=TheUserControl, Path=AppointmentTypes}"
SelectedItem="{Binding Path=Type}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView>
</UserControl>
You can do it by defining a property of type ObservableCollection.
If the data type of the collection is string then you can define it as:
public ObservableCollection<string> Types
{
get
{
ObservableCollection<string> _types = new ObservableCollection<string>();
_types.Add("value1");
_types.Add("value2");
_types.Add("value3");
return _types;
}
}
In your XAML you can bind it as:
<ComboBox VerticalAlignment="Top" HorizontalAlignment="Left" ItemsSource="{Binding Path=Types}" SelectedItem="{Binding MainViewModel.Type,Mode=TwoWay}" SelectedValue="{Binding SelectedValue}" Width="300" ></ComboBox>
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.