I'm trying display ListView grouping. I did as below:
ListView.xaml.cs
ListViewModel lvm = new ListViewModel();
lvUsers3.ItemsSource = lvm.getData();
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers3.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("Type");
view.GroupDescriptions.Add(groupDescription);
ListView.xaml
<ListView Name="lvUsers3" Margin="0,197,94,0" ItemsSource="{Binding tvq}">
<ListView.View>
<GridView>
<GridViewColumn Header="FirstName" Width="100" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="LastName" Width="100" DisplayMemberBinding="{Binding LastName}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="14" FontStyle="Normal" Text="{Binding Type}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
ListViewModel.cs
namespace LayoutMVVM.ViewModels
{
public class ListViewModel
{
public List<test_view> getData()
{
testViewClassDataContext tv = new testViewClassDataContext();
List<test_view> tvq = (from tt in tv.test_views
select tt).ToList();
return tvq;
}
}
}
On result I see that is grouped but the "type" value is missing (like was hidden?)
When you have applied the grouping you have created a group object that contains your sub items and this is what you are binding your group header to
the group object doesn't contain a Type property to bind to
try "{Binding Name}" instead of "{Binding Type}"
Related
I hava a textbox with should filter my listview but it doesn´t works.
The Data is correct but it doesn´t refresh the List.
XAML:
<TextBox x:Name="txtSearch" Text="{Binding Path=Filter,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
<ListView x:Name="Users" ItemsSource="{Binding Path=Items, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="SValue" SelectedValue="{Binding SelItem.SValue}" SelectedItem="{Binding SelItem}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=usertable_id}" Header="ID" Width="30"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=username}" Header="Username" Width="73"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=name}" Header="Full Name" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=filter}" Header="Jira Filter ID" Width="73"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=last_login}" Header="Last Loging" Width="100"/>
</GridView>
</ListView.View>
</ListView>
My ViewModel:
// Get Data from Database.
DataTable dt = new DataTable();
baseConfig = XMLUtils.OpenXMLFile<BaseInterfaceConfig>(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigParameterNames.Config));
Configuration config = new Configuration(baseConfig);
Database db = new Database(config.GetConnectionString(eDatabaseType.Customer));
var userData = db.GetObjectList<UserTableDTO>("Select usertable_id, username, name, company, email, filter, language, GetStatusMail, assigned_project_key, last_login from dbo.UserTable");
// Show DataTable "UserTable" in Listview.
view.Users.ItemsSource = userData;
// Filter
CvsStaff = new CollectionViewSource();
CvsStaff.Source = userData;
CvsStaff.Filter += ApplyFilter;
userData is filled with data from the Datatable.
I'm trying to group items on my list and sort them from oldest to newest. Grouping works perfect, it just doesn't want to inverse this list.
WPF Code:
<ListView x:Name="lst_orders" Margin="5" GridViewColumnHeader.Click="results_Click">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter Property="IsSelected" Value="true" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button x:Name="goto_parts" Content="Show parts" Width="AUTO" Padding="2" Margin="2" Background="#FF179917" Click="goto_parts_Click"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Order ID" Width="100" DisplayMemberBinding="{Binding fullOrderId}" />
<GridViewColumn Header="Job Status" Width="100" DisplayMemberBinding="{Binding status}" />
<GridViewColumn Header="Order Type" Width="150" DisplayMemberBinding="{Binding orderType}" />
<GridViewColumn Header="Customer Notes" Width="250" DisplayMemberBinding="{Binding notes}" />
<GridViewColumn Header="Admin Notes" Width="250" DisplayMemberBinding="{Binding adminNotes}" />
<GridViewColumn Header="Production Notes" Width="100" DisplayMemberBinding="{Binding production_notes}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Gray">
<TextBlock Text="Days: " FontSize="18" FontWeight="Bold"/>
<TextBlock Text="{Binding Name}" FontSize="18" FontWeight="Bold"/>
<TextBlock Text=" Systems: " FontSize="18" FontWeight="Bold"/>
<TextBlock Text="{Binding ItemCount}" FontSize="18" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
C# Code:
ICollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lst_orders.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("daysOld");
view.GroupDescriptions.Add(groupDescription);
view.SortDescriptions.Add(new SortDescription("daysOld", ListSortDirection.Descending));
lst_orders.ItemsSource = view;
daysOld is a property that calculates difference between today's date and order date, works perfect, but I want to see it from the highest number to the lowest. Picture shows what I get, and I want to reverse it.
Your code is correct.I've made a test. It is possible to see in view:
C#:
List<SomeClass> list = new List<SomeClass>();
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
list.Add(new SomeClass() { DaysOld = DateTime.Now.Add(new TimeSpan(0, rnd.Next(25), 0))});
}
ICollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(list);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("daysOld");
view.GroupDescriptions.Add(groupDescription);
view.SortDescriptions.Add(new SortDescription("DaysOld", ListSortDirection.Descending));
listBox.ItemsSource = view;
listBox.DisplayMemberPath = "DaysOld";
XAML:
<ListBox Name="listBox"/>
Model:
public class SomeClass
{
public DateTime DaysOld { get; set; }
}
Image:
So i have dataTemplate generated listbox like this:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Rectangle Fill="{Binding color}" Height="10" Width="10" Grid.Column="1"/>
<TextBlock Text=" " Grid.Column="2"/>
<TextBlock Text="{Binding ID}" FontSize="10" FontWeight="Bold" Grid.Column="3"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate
and I bind it to ObservableCollection. I need to access specific item in this listbox and change colour of its rectangle element.
I would use a StyleSelector. make a styleselector like this:
ListViewItemStyleSelector.cs
public class ListViewItemStyleSelector : StyleSelector
{
public override Style SelectStyle(object item,
DependencyObject container)
{
Style st = new Style();
st.TargetType = typeof(ListViewItem);
Setter backGroundSetter = new Setter();
backGroundSetter.Property = ListViewItem.BackgroundProperty;
ListView listView =
ItemsControl.ItemsControlFromItemContainer(container)
as ListView;
int index =
listView.ItemContainerGenerator.IndexFromContainer(container);
if (index % 2 == 0) <-- here your own criteria
{
backGroundSetter.Value = Brushes.LightBlue;
}
else
{
backGroundSetter.Value = Brushes.Beige;
}
st.Setters.Add(backGroundSetter);
return st;
}
}
use of the styleselector
xaml
<ListView
ItemsSource="{Binding Source={StaticResource EmployeeData},
XPath=Employee}"
ItemContainerStyleSelector="{DynamicResource myStyleSelector}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FirstName}"
Header="First Name" Width="120"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=LastName}"
Header="Last Name" Width="120"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FavoriteCity}"
Header="Favorite City" Width="120"/>
</GridView>
</ListView.View>
</ListView>
I have a ListView in my XAML defined as
<vm:JobListView x:Name="JobEditor" ItemsSource="{Binding Path=JobListViewSource}">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Width="10" HeaderContainerStyle="{DynamicResource FirstColumnGridViewColumnHeader}" CellTemplate="{StaticResource EmptyColumnTemplate}" />
<vm:SortListViewColumn Header="Description" Width="100" SortProperty="Description" SortStyle="JobDataGridViewColumnHeader" CellTemplate="{StaticResource DescriptionColumnTemplate}" />
<vm:SortListViewColumn Header="Staff" Width="100" SortProperty="Staff.FirstName" SortStyle="JobDataGridViewColumnHeader" CellTemplate="{StaticResource StaffColumnTemplate}" />
<vm:SortListViewColumn Header="Client" Width="50" SortProperty="Client.Name" SortStyle="JobDataGridViewColumnHeader" CellTemplate="{StaticResource ClientColumnTemplate}" />
<vm:SortListViewColumn Header="Samples" Width="100" SortProperty="Samples" SortStyle="JobDataGridViewColumnHeader" CellTemplate="{StaticResource SamplesNoColumnTemplate}" />
<vm:SortListViewColumn Header="Entry Date" Width="100" SortProperty="EntryDate" SortStyle="JobDataGridViewColumnHeader" CellTemplate="{StaticResource EntryDateColumnTemplate}" />
<GridViewColumn Width="10" CellTemplate="{StaticResource EmptyColumnTemplate}" />
</GridView>
</ListView.View>
</vm:JobListView>
vm:JobListView is a class that inherits from ListView
The CellTemplates are defined like this:-
<!-- description column -->
<DataTemplate x:Key="DescriptionColumnTemplate">
<TextBox Style="{StaticResource ReadOnlyTextBlockStyle}" Text="{Binding Path=Description}" />
</DataTemplate>
The ItemsSource is bound to a property on my VM defined like this:-
public ICollectionView JobListViewSource
{
get { return Get(() => JobListViewSource); }
set { Set(() => JobListViewSource, value); }
}
and I set the property like this:-
CollectionViewSource source = new CollectionViewSource();
source.Source = this.Jobs;
this.JobListViewSource = source.View;
where Jobs is an ObservableCollection of Job objects
What I get displayed in my grid is just the namespace of the Job object and not the individual properties in each column
What am I doing wrong?
Try using
this.JobListViewSource.SourceCollection=this.Jobs instead of
CollectionViewSource source = new CollectionViewSource();
source.Source = this.Jobs;
this.JobListViewSource = source.View;
This is my code for generating the gridview
User u1 = new User();
User u2 = new User();
for (int i = 0; i < files.Count; i++)
{
u1.Files = files[i];
items.Add(u1);
}
for (int i = 0; i < sharedFiles.Count; i++)
{
u2.Files += sharedFiles[i];
items.Add(u2);
}
gridview1.ItemsSource = items;
}
public class User
{
public string Files { get; set; }
}
This is my xaml page
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="250" DisplayMemberBinding="{Binding Files}" ></GridViewColumn>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Download" Click="fileDownloadClick" CommandParameter="{Binding Files}"></Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Share" Click="fileShareClick" CommandParameter="{Binding Files}" ></Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
I would like to know if its possible to only allow my Share buttons disabled in my 2nd for loop (u2) while my Share buttons in (u1) is still enabled. Currently both are enabled. I am currently using this to retrieve values for particular role
private void fileShareClick(object sender, RoutedEventArgs e)
{
object share = ((Button)sender).CommandParameter;
}
Create a DataTrigger on the Button which you want. and bind that with property that represent the boolen type value. like IsShareAllowed.
Try some thing like this.
var procs = (from p in System.Diagnostics.Process.GetProcesses()
select new
{
Name = p.ProcessName,
IsShareAllowed = Convert.ToBoolean(r.Next(-1, 1))
}).ToList(); ;
list.ItemsSource = procs;
Xaml
<ListView Name="list" Grid.Row="4">
<ListView.View>
<GridView >
<GridView.Columns>
<GridViewColumn Width="250" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Download" Click="fileDownloadClick" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Share" Click="fileShareClick" >
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding IsShareAllowed}" Value="false">
<Setter Property="IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>