sort and group Icollectionview in WPF - c#

I have created a listview control in WPF and has success fully bound Icollectionview object of ObservableCollection<object>. My listview columns are created dynamically.
I have to sort and group my listview and it is not working properly.
My code is as below.
private void LaodList()
{
dt = new DataTable();
dt.Columns.Add("AA", typeof(string));
dt.Columns.Add("BB", typeof(string));
dt.Columns.Add("cc", typeof(string));
dt.Rows.Add("12", "66",11);
dt.Rows.Add("33", "44",22);
dt.AcceptChanges();
GridView gv = new GridView();
//gv.AllowsColumnReorder = true;
List<string> myItemsCollection = new List<string>();
for (int i = 0; i < dt.Columns.Count; i++)
{
GridViewColumn col = new GridViewColumn();
col.Header = dt.Columns[i].ColumnName;
col.DisplayMemberBinding = new Binding(string.Format("[{0}]", i));
gv.Columns.Add(col);
myItemsCollection.Add(col.Header.ToString());
}
LvItems.View = gv;
this.Source = CollectionViewSource.GetDefaultView(LoadItems(dt)) ;
LvItems.DataContext = this.Source;
cmbGroups.ItemsSource = myItemsCollection;
}
public ObservableCollection<object> LoadItems(DataTable dt)
{
ObservableCollection<object> items = new ObservableCollection<object>();
foreach (DataRow dataRow in dt.Rows)
{
items.Add(dataRow.ItemArray);
}
return items;
}
//sort////////////////////////
private void ListView_Click(object sender, RoutedEventArgs e)
{
GridViewColumnHeader currentHeader = e.OriginalSource as GridViewColumnHeader;
if (currentHeader != null && currentHeader.Role != GridViewColumnHeaderRole.Padding)
{
if (this.Source.SortDescriptions.Count((item) => item.PropertyName.Equals(currentHeader.Column.Header.ToString())) > 0)
{
SortDescription currentPropertySort = this.Source
.SortDescriptions
.First<SortDescription>(item => item.PropertyName.Equals(currentHeader.Column.Header.ToString()));
//Toggle sort direction.
ListSortDirection direction =
(currentPropertySort.Direction == ListSortDirection.Ascending) ?
ListSortDirection.Descending : ListSortDirection.Ascending;
//Remove existing sort
this.Source.SortDescriptions.Remove(currentPropertySort);
this.Source.SortDescriptions.Insert(0, new SortDescription(currentHeader.Column.Header.ToString(), direction));
}
else
{
this.Source.SortDescriptions.Insert(0, new SortDescription(currentHeader.Column.Header.ToString(), ListSortDirection.Ascending));
}
this.Source.Refresh();
}
}
//group////////////////////
private void btnGroup_Click(object sender, RoutedEventArgs e)
{
this.Source.GroupDescriptions.Clear();
PropertyInfo pinfo = typeof(object).GetProperty(cmbGroups.Text);
if (pinfo != null)
this.Source.GroupDescriptions.Add(new PropertyGroupDescription(pinfo.Name));
}
WPF code is as below
<ListView ItemsSource="{Binding}" x:Name="LvItems" ButtonBase.Click="ListView_Click" IsSynchronizedWithCurrentItem="True" Grid.Row="1" Margin="0,22,0,43">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontSize="15" FontWeight="Bold" Text="{Binding}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>

Since you are using WPF you shall use DataBindings. Expose your ICollectionView as a property and bind on it
public ICollectionView MyList
{
get
{
if(_mylist == null)
_mylist = CollectionViewSource.GetDefaultView(observableCollection);
return _mylist;
}
}
In the XAML you apply the binding as follows
<ListView ItemsSource="{Binding Path=MyList}"/>
And now you apply the sorting on that property
MyList.SortDescriptions.Remove(...);
MyList.SortDescriptions.Add(...);
MyList.GroupDescription.Add(...);
This has the drawback that every Remove or Add on a SortDescription or GroupDescription will refresh the ListView. Normally this is unwanted if you want to apply many sortings in one step. Then you should enclose the block with following:
using(MyList.DeferRefresh())
{
//put your changes in sorting and grouping here
}

Related

WPF C# Get Cell value from autogenerated Data Grid and Data Table

I have created simple mechanism for data table xaml would looks simply:
<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}" AutoGenerateColumns="True" />
And MVVM code behind is based on data tables, and as well fairly simple:
private void GenerateDataView()
{
CurrentsFlagValuesView = new DataTable();
CurrentsFlagValuesView.Columns.Add("Bits");
var bitLength = 0;
foreach (CurrentsFlagAnalysis flag in CurrentsFlagValues)
{
CurrentsFlagValuesView.Columns.Add(flag.DailyCurrentsTimestampInterval.ToString("yyyy-MM-dd"));
bitLength = flag.CurrentFlagsLength;
}
for (var bit = 0; bit < bitLength; bit++)
{
List<CurrentFlagEventEnum> flags = CurrentsFlagValues
.Select(value => value.CurrentFlags.ElementAt(bit))
.Select(value => value ? (CurrentFlagEventEnum)bit + 1 : CurrentFlagEventEnum.None)
.ToList();
var dataRowValues = new List<object> { bit };
dataRowValues.AddRange(flags.Cast<object>());
CurrentsFlagValuesView.Rows.Add(dataRowValues.ToArray());
}
}
But I came upon a problem, or two I want to get data of the cell when I click the cell, like Column title, and value of the cell. I managed to do this without MVVM like:
void EditingDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
DataGridCell Cell = EditingDataGrid.GetCurrentDataGridCell();
var Position = Cell.PointToScreen(new Point(0, 0));
TextBlock text = (TextBlock)Cell.Content;
MessageBox.Show("Value=" + text.Text, "Position" );
}
public static DataGridCell GetCurrentDataGridCell(this DataGrid dataGrid)
{
DataGridCellInfo cellInfo = dataGrid.CurrentCell;
if (cellInfo.IsValid == false)
{
return null;
}
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent == null)
{
return null;
}
return cellContent.Parent as DataGridCell;
}
But now I want to remodel that to that pattern, but I do not know how. Any Ideas how to bind command to that?
You could bind the CurrentCell property of the DataGrid to a DataGridCellInfo (not DataGridCell) source property provided that you set the Mode of the Binding two TwoWay:
<DataGrid ItemsSource="{Binding CurrentsFlagValuesView}"
CurrentCell="{Binding CurrentCell, Mode=TwoWay}"
AutoGenerateColumns="True" />
Then the source property of the view model will be set whenever you select a cell in the view and you could simply move your current logic to the view model:
private DataGridCellInfo _currentCell;
public DataGridCellInfo CurrentCell
{
get { return _currentCell; }
set { _currentCell = value; OnCurrentCellChanged(); }
}
void OnCurrentCellChanged()
{
DataGridCell Cell = GetCurrentDataGridCell(_currentCell);
var Position = Cell.PointToScreen(new Point(0, 0));
TextBlock text = (TextBlock)Cell.Content;
MessageBox.Show("Value=" + text.Text, "Position");
}
public static DataGridCell GetCurrentDataGridCell(DataGridCellInfo cellInfo)
{
if (cellInfo == null || cellInfo.IsValid == false)
{
return null;
}
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent == null)
{
return null;
}
return cellContent.Parent as DataGridCell;
}
You could also wrap this functionality in a behaviour that sets the source property of the view model to the actuall cell value:
https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF
https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/
You can simply bind the current cell property in the view model and you will have the current cell with you all the time:
<DataGrid AutoGenerateColumns="True"
SelectionUnit="Cell"
CanUserDeleteRows="True"
ItemsSource="{Binding Results}"
CurrentCell="{Binding CellInfo}"
SelectionMode="Single">
In the view model:
private DataGridCell cellInfo;
public DataGridCell CellInfo
{
get { return cellInfo; }
}

Adding button click event in datagridtemplatecolumn in user control from mainwindow programmatically

I have a MainWindow. It has a stackpanel myStack and some other things.
In stackPanel, there is a usercontrol (TaskGrid(_TG)) added programmatically.
In this UserControl, there is a DataGrid(dgEmployee), which have 4 template columns. the last column contains a button.
I am trying to assign the button click event from the mainwindow constructor and handle the event here.
Here are the codes:
in MainWindow.xaml
<Grid Grid.Row="2">
<StackPanel Name="myStack"/>
</Grid>
in MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
_TG = new TaskGrid();
_TD = new _1.TaskDetails();
_TM = new _1.TaskMaster();
myStack.Children.Add(_TG);
_AUC = ActiveUserControl.Grid;
foreach (object child in myStack.Children)
{
string childname = "";
if (child is FrameworkElement)
{
childname = (child as FrameworkElement).Name;
if (childname == "TaskGrid")
{
Grid dg = ((Grid)((UserControl)child).Content);
foreach (var item in dg.Children)
{
DataGridColumn b = ((DataGrid)item).Columns[3] as DataGridColumn;
}
}
}
}
}
And in TaskGrid.xaml, the only template column is given here
<DataGridTemplateColumn Width="30">
<DataGridTemplateColumn.CellTemplate>
<ItemContainerTemplate>
<Button Name="btnMaster" Background="Transparent">
<Button.Template>
<ControlTemplate>
<Image Source="ArrowRight.png"/>
</ControlTemplate>
</Button.Template>
</Button>
</ItemContainerTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I have to assign the click event like
button.click += new RoutedEvent(button_click);
And later use button_click event in the MainWindow.xaml.cs
Wait until the UserControl has been loaded. You could then get a reference to the DataGrid using the following helper method that searches for an element of a specific type recursively in the visual tree.
private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null)
return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
The same way you can get a reference to a specific cell:
public static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
{
if (rowContainer != null)
{
System.Windows.Controls.Primitives.DataGridCellsPresenter presenter =
GetChildOfType<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
if (presenter != null)
return presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
}
return null;
}
Please refer to the following blog post for more information about this: https://blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or-cell-in-a-datagrid-in-wpf/
Below is a full example for you. Note that the DataGrid may contain several rows and some of the rows may have been virtualized away. You will find more information about this on the link above.
public MainWindow()
{
InitializeComponent();
_TG = new TaskGrid();
_TD = new _1.TaskDetails();
_TM = new _1.TaskMaster();
myStack.Children.Add(_TG);
_AUC = ActiveUserControl.Grid;
_TG.Loaded += (s, e) =>
{
DataGrid dataGrid = GetChildOfType<DataGrid>(_TG);
if (dataGrid != null)
{
foreach (var item in dataGrid.Items)
{
DataGridRow dgr = dataGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (dgr != null)
{
DataGridCell cell = GetCell(dataGrid, dgr, 3); //<-- column index
if (cell != null)
{
Button button = GetChildOfType<Button>(cell);
if (button != null)
{
button.Click += new RoutedEvent(button_click);
}
}
}
}
}
};
}

default selection is not working in wpf

here i m using a wpf ComboBox control and binding it using the datasource.
but my combobox is unable to set default seletion of first index which i give
manualy during the time to binding. here my code shown below can any one tell me how to set default item in combox box.
//Xaml
<ComboBox Height="23" HorizontalAlignment="Left" Margin="142,11,0,0" Name="cmbProductType" VerticalAlignment="Top" Width="180" ItemsSource="{Binding}" />
//Code
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ClsControl.GetProductTypeList(cmbProductType);
}
public static void GetProductTypeList(ComboBox ddlProductType)//Add By Sandeep On 11-03-2013
{
try
{
DataTable dtProductType = null;
try
{
ClsDataLayer objDataLayer = new ClsDataLayer();
dtProductType = objDataLayer.ExecuteDataTable("COMNODE_PROC_GetProductTypeList");
if (dtProductType != null && dtProductType.Rows.Count > 0)
{
DataRow drCardType = dtProductType.NewRow();
drCardType[0] = -1;
drCardType[1] = "< -- Select Card Type -- >";
ddlProductType.SelectedValue = -1;
dtProductType.Rows.InsertAt(drCardType, 0);
ddlProductType.ItemsSource = dtProductType.DefaultView;
ddlProductType.DisplayMemberPath = "PRODUCT_TYPE";
ddlProductType.SelectedValuePath = "PRODUCT_TYPE_ID";
}
}
catch (Exception)
{
throw;
}
}
catch
{
}
}
Try updating your XAML to this:
<ComboBox Height="23"
HorizontalAlignment="Left"
Margin="142,11,0,0"
Name="cmbProductType"
VerticalAlignment="Top"
Width="180"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True" />
Also, use SelectedIndex instead when you select the item:
ddlProductType.SelectedIndex = 0;
dtProductType.Rows.InsertAt(drCardType, 0);
ddlProductType.ItemsSource = dtProductType.DefaultView;
ddlProductType.DisplayMemberPath = "PRODUCT_TYPE";
ddlProductType.SelectedValuePath = "PRODUCT_TYPE_ID"

Get selected row item in DataGrid WPF doesn't work

Im tring to get Selected row item.
I have been readed that it should be worked :
<DataGrid ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/>
Customer customer = (Customer)myDataGrid.SelectedItem;
In the first xaml - I put it, it doesnt error or something I just dont know how to use it.. How in the c# code I can get the selected row ?
In the C# line code It error. The visual studio doesn't exist Customer.
I would thankfull to help. :) Thanks.
rly hard to know what exactly you wanted :) but here is an example I just put togetter on how to drag info out of the datagrid.
Did not implament alot of those bindings in it so this is pure getting the info.
Xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" Name="dg"/>
<Button Grid.Row="1" Name="btn" Click="btn_Click" />
</Grid>
Codebehind
List<SomeInfo> list = new List<SomeInfo>();
public MainWindow()
{
InitializeComponent();
list.Add(new SomeInfo() { Name = "PC", Description = "Computer", ID = 1 });
list.Add(new SomeInfo() { Name = "PS", Description = "Playstation", ID = 2 });
list.Add(new SomeInfo() { Name = "XB", Description = "Xbox", ID = 3 });
this.dg.ItemsSource = list;
}
public class SomeInfo
{
public string Name { get; set; }
public string Description { get; set; }
public int ID { get; set; }
}
private void btn_Click(object sender, RoutedEventArgs e)
{
if (dg.SelectedIndex != -1)
{
DataGrid dataGrid = sender as DataGrid;
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(dg.SelectedIndex);
DataGridCell RowColumn = dg.Columns[0].GetCellContent(row).Parent as DataGridCell;
btn.Content = ((TextBlock)RowColumn.Content).Text;
}
}
the btn_click does all the gathering info stuff where the last 2 makes my datagrid for testing.
Hope it will help you out :)
------------------Edit--------------------------
from the comment this below is what you would need only
private void btn_Click(object sender, RoutedEventArgs e)
{
if (dg.SelectedIndex != -1)
{
DataGrid dataGrid = sender as DataGrid;
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(dg.SelectedIndex);
DataGridCell RowColumn = dg.Columns[0].GetCellContent(row).Parent as DataGridCell;
btn.Content = ((TextBlock)RowColumn.Content).Text;
}
}
dg = your datagrid
dg.Columns[0] = change 0 into what column you want
info from btn.Content = what you want the content to be
--------------EDIT 2------------
To get the selected row's index all you need is
int index = dg.SelectedIndex;
btn.Content = index;
or if you dont want to store the integar
btn.Content = dg.SelectedIndex;
The grid defined in a very incomplete.
Council to explicitly all columns that compose it with the individual binding
In this case I used the event doubleClick but you have to choose the right event to you
By code for example try this:
In XAML add:
<DataGrid x:Name="MyDataGrid" x:FieldModifier="public" MouseDoubleClick="MyDataGrid_MouseDoubleClick" ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/>
And in behind c# code this:
private void MyDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (sender != null)
{
DataGrid grid = sender as DataGrid;
if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
{
var selectedRow = grid.SelectedItem as MyObject;
try
{
Mouse.OverrideCursor = Cursors.Wait;
//TODO YOUR OPERATION ON selectdRow
}
finally
{
Mouse.OverrideCursor = null;
}
}
}
}

Dynamically update a data bound listbox C# WPF

So despite finding articles online I still cannot figure this out.
I have a Listbox
<ListBox HorizontalAlignment="Left" Margin="54,35,0,0" Name="resultsbox" VerticalAlignment="Top" Width="382" Visibility="Collapsed">
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding nameElement}"/>
</StackPanel>
</DataTemplate>
</ListBox>
That is databound to
ObservableCollection<string> results = new ObservableCollection<string>();
and is updated with
private void searchbox_TextChanged(object sender, TextChangedEventArgs e)
{
resultsbox.Visibility = Visibility.Visible;
resultsbox.ItemsSource = results;
if (results.Count == 0)
{
foreach (ele item in eles)
{
if (!results.Contains(item.nameElement))
{
results.Add(item.nameElement);
}
}
}
else
{
resultsbox.Items.Clear();
}
if (searchbox.Text.Equals(""))
{
window1.Height = 47;
resultsbox.Visibility = Visibility.Collapsed;
}
if (resultsbox.Items.Count == 0)
{
resultsbox.Visibility = Visibility.Collapsed;
window1.Height = 47;
}
else{
window1.Height = 47 + (22 * resultsbox.Items.Count);
}
}
It loads ALL the data in there but WILL NOT UPDATE!
If I do resultsbox.clear() it says you can't clear bound items. If you try and clear the source it does nothing. If you try and set the resultsbox itemsource to null and clear the source then rebind it, nothing. If you try and bind the listbox to an empty source it does nothing....
The answer was changing the foreach loop in the update from
resultsbox.ItemsSource = results;
if (results.Count == 0)
{
foreach (ele item in eles)
{
if (!results.Contains(item.nameElement))
{
results.Add(item.nameElement);
}
}
}
to
results.Clear();
foreach (ele item in eles)
{
if (item.nameElement.ToLower().Contains(searchbox.Text.ToLower()))
{
results.Add(item.nameElement);
}
}
resultsbox.ItemsSource = results;
You can try using Two-Way Mode Binding to achieve your requirement IMO,
<ListBox HorizontalAlignment="Left" Margin="54,35,0,0" Name="resultsbox" VerticalAlignment="Top" Width="382" Visibility="Collapsed">
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding nameElement, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>

Categories