XAML Binding Observable Collection to DataGrid - c#

I'm having trouble databinding based on other properties. My combobox is working fine, but the datagrids aren't. I'm was trying to bind the window to the codebehind using
DataContext="{Binding RelativeSource={RelativeSource Self}}" so I could have design time Intellisense, but I sort of got it using x:Name:"_Window"
The model for HRRM has lots of entities, and I know that that the properties I'm trying to bind too are spelt correctly etc. But I can't figure out why the DataGrid won't show the employees. I've also ensured that the data is being placed in the observable collection, and I've tried binding the items source just to ListEmployees and SmsEmployees and nothing I've tried has worked. This is the last bit of code I have tried.
<Window x:Class="GUI.Employees.Misc.SendSms"
x:Name="_Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Send SMS" Height="525" Width="1000" DataContext="{Binding ElementName=_Window}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="270*" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="270*" />
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0" x:Name="cmbCompany" DisplayMemberPath="Name" ItemsSource="{Binding Path=Companies, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Margin="10,10,10,0" VerticalAlignment="Top" SelectionChanged="cmbCompany_SelectionChanged"/>
<DataGrid Grid.Column="0" x:Name="dgEmployees" Margin="10,37,10,10" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Path=ListEmployees, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window} }">
<DataGrid.Columns>
<DataGridTextColumn Header="Company" Binding="{Binding HRCo }"/>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Craft" Binding="{Binding StdCraft}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dgSendSms" Margin="10,37,10,10" IsReadOnly="True" Grid.Column="2" ItemsSource="{Binding SmsEmployees, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window} }">
<DataGrid.Columns>
<DataGridTextColumn Header="Company" Binding="{Binding HRCo}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
<DataGridTextColumn Header="Craft" Binding="{Binding StdCraft}"/>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="cmdMoveAllHired" Content=">>" Margin="10,85,10,0" VerticalAlignment="Top" Grid.Column="1"
Click="cmdMoveAllHired_Click" />
<Button x:Name="cmdReturnSingleItem" Content=">" Margin="10,122,10,0" VerticalAlignment="Top"
Grid.Column="1" Click="cmdReturnSingleItem_Click" />
<Button x:Name="cmdMoveAllReturned" Content="<<" Margin="10,196,10,0" VerticalAlignment="Top"
Grid.Column="1" Click="cmdMoveAllReturned_Click" />
<Button x:Name="cmdHireSingleItem" Content="<" Margin="10,159,10,0" VerticalAlignment="Top" Grid.Column="1"
Click="cmdHireSingleItem_Click" />
<Button x:Name="cmdGenerate" Content="Generate" Grid.Column="1" Margin="10,0,10,10"
VerticalAlignment="Bottom" Click="cmdGenerate_Click" Visibility="Collapsed" />
<Button x:Name="cmdBack" Content="Back" Grid.Column="1" HorizontalAlignment="Left" Margin="10,249,0,0"
VerticalAlignment="Top" Width="130" Click="cmdBack_Click" />
<Button x:Name="cmdSendSMS" Content="Send SMS" Grid.Column="1" HorizontalAlignment="Left" Margin="10,316,0,0" VerticalAlignment="Top" Width="130" Click="cmdSendSMS_Click"/>
</Grid>
namespace GUI.Employees.Misc
{
public partial class SendSms
{
public ObservableCollection<HQCO> Companies { get; set; }
public ObservableCollection<HRRM> ListEmployees { get; set; }
private ObservableCollection<HRRM> _smsEmployees;
public ObservableCollection<HRRM> SmsEmployees {
get { return _smsEmployees; }
set { _smsEmployees = value;
}
}
public SendSms()
{
InitializeComponent();
Companies = new ObservableCollection<HQCO>(HQCO.GetActivePRCompanies());
Companies.Insert(0, new HQCO { HQCo = 0, Name = "All" });
// cmbCompany.SelectedItem = _companies.Single(x=>x.HQCo == 0);
SmsEmployees = new ObservableCollection<HRRM>();
ChangeCompany();
}
private void ChangeCompany()
{
if (((HQCO)cmbCompany.SelectedItem)?.HQCo == 0)
foreach (var co in Companies)
co.IsChecked = true;
else
foreach (var co in Companies)
if (((HQCO)cmbCompany.SelectedItem) == co)
co.IsChecked = true;
else
co.IsChecked = false;
ListEmployees = new ObservableCollection<HRRM>(Facade.GetEmployeePhoneNumbers(Companies.ToList(), false).OrderBy(x => x.SortName));
}
}

I think your problem is how you assign the DataContext of your Window. In your XAML, add a reference to the namespace so the Window can find your SendSms viewmodel, and then either assign the DataContext property in XAML (see below), or in the code-behind of your Window. If you set the DataContext this way, you shouldnt need to use RelativeSource when binding to the SendSms viewmodel, but if you want see changes, you also need to make sure the classes your observable collections are populated with implements INotifyPropertyChanged.
<Window x:Name="_Window"
xmlns:local="clr-namespace:GUI.Employees.Misc" >
<!-- set the DataContext of this Window to an instance of SendSms -->
<Window.DataContext>
<local:SendSms />
</Window.DataContext>
<DataGrid ItemsSource="{Binding ListEmployees}">
...
</DataGrid>

I don't believe your ListEmployees ItemsSource will update the DataGrid because it is not a DependencyProperty or you have not implemented INotifyPropertyChanged. It is not updating because you are assigning the property a new instance.
Instead try adding this to your constructor:
ListEmployees = new ObservableCollection<HRRM>();
And this to your ChangeCompany method:
foreach (var employee in Facade.GetEmployeePhoneNumbers(Companies.ToList(), false).OrderBy(x => x.SortName))
{
ListEmployees.Add(employee);
}
The above will actually make use of the ObservableCollection and it should then update the UI.

Related

Multiple Selection From DataGrid WPF

im trying to select multiple rows/items from a datagrid
<DataGrid IsReadOnly="True" SelectionMode="Extended" SelectionUnit="FullRow" AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource ClientiVS}}" SelectedItem="{Binding Path=ClienteSelezionato}" RowHeaderWidth="0" EnableColumnVirtualization="False" CanUserReorderColumns="False" EnableRowVirtualization="False" CanUserAddRows="false" ColumnWidth="*" FontFamily="Agency FB" FontSize="22" Name="DataGrid1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionChanged="DataGrid1_SelectionChanged" Grid.Row="1" Margin="30,10,30,30">
XAML DATAGRID CODE
<DataGrid.Columns>
<DataGridTextColumn Width="100" FontFamily="Agency FB" FontSize="20" Header="Nome" Binding="{Binding Nome}" ></DataGridTextColumn>
<DataGridTextColumn Width="100" FontFamily="Agency FB" FontSize="20" Header="Cognome" Binding="{Binding Cognome}" ></DataGridTextColumn>
<DataGridTextColumn Width="150" FontFamily="Agency FB" FontSize="20" Header="Indirizzo" Binding="{Binding Indirizzo}"></DataGridTextColumn>
<DataGridTextColumn Width="150" FontFamily="Agency FB" FontSize="20" Header="Telefono" Binding="{Binding NumeroTelefono}"></DataGridTextColumn>
<DataGridTextColumn Width="1*" FontFamily="Agency FB" FontSize="20" Header="Email" Binding="{Binding Email}"></DataGridTextColumn>
<DataGridCheckBoxColumn Header="Selezionato" Binding="{Binding ClienteSelezionato}"/>
</DataGrid.Columns>
</DataGrid>
DATAGRID SELECTIONCHANGED EVENT CODE:
public Cliente ClienteSelezionato { get; set; }
private ObservableCollection<Cliente> _ListaClientiSelezionati;
public ObservableCollection<Cliente> ListaClientiSelezionati { get { return _ListaClientiSelezionati; } set { _ListaClientiSelezionati = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ListaClientiSelezionati")); } }
private void DataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ClienteSelezionato != null )
{
ListaClientiSelezionati.Add(ClienteSelezionato);
}
}
The problem is that: if i select two times the same Row from the DataGrid("ClienteSelezionato") it will obviously insert into the list the same item twice. How can i fix it ?
I just want to implement the control that tells me if an item is already contained in the list ("ListaClientiSelezionati")
Thanks!

Add a buttons to rows of DataGrid and Event_Click to each using Caliburn micro in WPF

I am trying to add a button "View" to to a series of rows (last cell of the row) of factures in DataGrid (using c#, WPF, Caliburn micro mvvm). Each button should have a handler that views the details of that facture in another form.
Questions:
How can I add this button at row ends?
How can I add the event handler?
I will only view the function that I am using to add the word "View" at the last column of each row.
I tried adding a button using: Button btn = new Button(); And set its required parameters. But that didn't work.
I will share the whole xaml and ViewModel files:
<UserControl x:Class="Factures.Views.FactureView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Factures.Views"
xmlns:cal="http://www.caliburnproject.org"
cal:Bind.Model="Shell"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" FontFamily="Times New Roman">
<Grid IsEnabled="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<DataGrid AlternatingRowBackground="Gray"
Grid.Column="1" Grid.Row="1"
x:Name="Factures"
ItemsSource="{Binding Factures}"
CanUserAddRows="False"
MinWidth="100" MinHeight="50"
FontFamily="Times New Roman"
AutoGenerateColumns="False"
IsReadOnly="True"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" />
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
<DataGridTemplateColumn Header="View">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="BtnView"
cal:Message.Attach="[Event Click]=[BtnView($dataContext)]"
Content="View Facture">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
using Caliburn.Micro;
using Factures.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Factures.ViewModels
{
public class FactureViewModel : Conductor<object>
{
public BindableCollection<FactureModel> Factures { get; set; }
public FactureViewModel()
{
FactureModel facture = new FactureModel();
DataTable allFactures = facture.All();
//Fill Bindable collection
this.FillAllFactures(allFactures);
}
public void BtnView(object row)
{
MessageBox.Show("Hello");
}
private void FillAllFactures(DataTable factures)
{
List<FactureModel> output = new List<FactureModel>();
foreach(DataRow row in factures.Rows)
{
FactureModel fm = new FactureModel
{
Id = System.Convert.ToInt32(row[0].ToString()),
Name = row[1].ToString(),
};
output.Add(fm);
}
this.Factures = new BindableCollection<FactureModel>(output);
}
}
}
You can use DataGridTemplateColumn and Caliburn Micro's Action message syntax to use a Button for each row in your datagrid.
For first part of your question, you can add a button to each row of your Datagrid using DataGridTemplateColumn.
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="View" cal:Message.Attach="[Event Click]=[ViewUser($dataContext)]"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Complete definition of DataGrid in your XAML would look like.
<DataGrid ItemsSource="{Binding Users}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="Id"/>
<DataGridTextColumn Binding="{Binding UserName}" Header="UserName"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="View" cal:Message.Attach="[Event Click]=[ViewUser($dataContext)]"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The cal:Message.Attach="[Event Click]=[ViewUser($dataContext)]" would use the Caliburn.Micro Action syntax to enable you to invoke a method on Click event of each button. The $dataContext parameter allows you to pass the corresponding Selected Data Item as parameter to the Method.
Your ViewModel method would look like
public void ViewUser(object user)
{
// Do work
}
You can read more on Caliburn.Micro Actions here.

Get hidden value from wpf datagrid

Can i get value Hidden column from DataGrid?
<DataGridTextColumn Header="" Width="10" Binding="{Binding id}" Visibility="Hidden"/>
Using this code, i get exception.
Data.IdServ = ((TextBlock)DataGridService.Columns[1].GetCellContent(row)).Text;
if (dgUserEnroll.SelectedItem != null)
{
var data = (User)dgUserEnroll.SelectedItem;
var userID = data.UserId;
}
this is a not option, because i have tableadapter when receiveng data
You can use your code behind too. You just need to hide the column in a different way:
<DataGridTextColumn Header="" MaxWidth="0" Binding="{Binding id}" />
i.e. remove the Visibility attribute and set MaxWidth to zero.
You have a Binding with id field, so use it instead of accessing cell content.
I found two ways
first
string ID = ((DataRowView)DataGridService.SelectedItem).Row["id"].ToString();
second
var data = (DataRowView)DataGridService.SelectedItem;
var userId = data.Row["id"];
First convert selected item of data grid view to ItemsSource of data grid view:
dataGridUser.ItemsSource is View_Users ==>
dataGridUser.ItemsSource = database.Database.SqlQuery<View_Users>(
"select * from view_users where 1=1"+searchString()).ToList();
Then, to get value Hidden or Visible column from DataGrid:
var id= ((View_Users)dataGridUser.SelectedItem).UserID;
I came with a simpler solution, suppose you have bound a List to the Datagrid, YourClass has Id property the XAML would look like :
<DataGrid x:Name="ListeUrls" AutoGenerateColumns="False" Margin="1,0,-1,27" >
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="Vendor" Binding="{Binding Vendor}" Foreground="red" FontWeight="Bold" ></DataGridTextColumn>
<DataGridTextColumn Header="Url" Binding="{Binding url}" ></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
In the code behind :
Somewhere you have bound the ListeUrls:
ListeUrls.ItemsSource = new List{ .... };
private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var dataGridCellTarget = (DataGridCell)sender;
var parent = VisualTreeHelper.GetParent(dataGridCellTarget);
.....
}
You use the VisualTreeHelper to get the parent of the cell on which you triggered the mouse doubleclick. The parent has the properties of YourClass thus the Id.
I think no need to set the width of the hidden DataGridColumn, set its Visibility to Hidden is enough.
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="delete" Width="40" Click="Delete_Click" Background="#FFD80000">
<materialDesign:PackIcon Kind="Delete" Width="25" Margin="-10,0,0,0" FontWeight="Bold"></materialDesign:PackIcon>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Edit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="edit" Width="40" Click="Edit_Click" Background="#FF673AB7">
<materialDesign:PackIcon Kind="Edit" Width="25" Margin="-10,0,0,0" FontWeight="Bold"></materialDesign:PackIcon>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTextColumn Width="300" Binding="{Binding [2]}" Header="Price"></DataGridTextColumn>-->
</DataGrid.Columns>

How to get a check-box in grid header

With this application, first column in grid is check box type with its header says Select. I would like the header to show a check box also. Checking/Unchecking that check box should check or uncheck all items in the grid. How can I do that?
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Loaded="Data_Loaded" >
<Grid.RowDefinitions>
<RowDefinition Height="6*" />
<RowDefinition />
</Grid.RowDefinitions>
<DataGrid x:Name="grEmployees" HorizontalAlignment="Left" Margin="10,10,0,0" CanUserAddRows="False" CanUserDeleteRows="False"
VerticalAlignment="Top" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" Grid.Row="0">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Select" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkSelectedDevice" IsChecked="{Binding Path=Configure, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName, Mode=OneWay}" Width="3*" />
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName, Mode=OneWay}" Width="2*" />
<DataGridTextColumn Header="Description" Binding="{Binding Description, Mode=OneWay}" Width="5*" />
</DataGrid.Columns>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5" Grid.Row="1" >
<Button x:Name="btnClose" Content="Close" Margin="5" Width="50" />
</StackPanel>
</Grid>
</Window>
public partial class MainWindow : Window
{
private List<Employee> Employees = null;
public MainWindow()
{
InitializeComponent();
}
private void Data_Loaded(object sender, RoutedEventArgs e)
{
Employees = new List<Employee>()
{
new Employee() { IsHardWorking = false, LastName = "Silly", FirstName = "Dude", Description= "this due is a mess" },
new Employee() { IsHardWorking = true, LastName = "Mean", FirstName = "Person", Description= "funny" },
new Employee() { IsHardWorking = false, LastName = "New", FirstName = "Friend", Description= "let her go in next round of layoffs" },
new Employee() { IsHardWorking = true, LastName = "My", FirstName = "Buddy", Description= "simply no comments" },
};
this.grEmployees.ItemsSource = Employees;
}
}
Right, so luckily there's nifty built in ways to customize all sorts of things. In this case we'll just override the default column header template with our own and plop a CheckBox in there.
<DataGridTemplateColumn.Header>
<CheckBox Name="ACheckBox"
Checked="Do_Something"
Unchecked="Do_Something_Else"/>
</DataGridTemplateColumn.Header>
Hope this helps. Cheers
<Grid Loaded="Data_Loaded" >
<Grid.RowDefinitions>
<RowDefinition Height="6*" />
<RowDefinition />
</Grid.RowDefinitions>
<DataGrid x:Name="grEmployees" HorizontalAlignment="Left" Margin="10,10,0,0" CanUserAddRows="False" CanUserDeleteRows="False"
VerticalAlignment="Top" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" Grid.Row="0">
<DataGrid.Columns>
<DataGridTextColumn>
<DataGridTextColumn.Header>
<StackPanel Orientation="Horizontal">
<CheckBox></CheckBox>
<TextBlock>Test</TextBlock>
</StackPanel>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTemplateColumn Header="Select" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkSelectedDevice" IsChecked="{Binding Path=Configure, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName, Mode=OneWay}" Width="3*" />
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName, Mode=OneWay}" Width="2*" />
<DataGridTextColumn Header="Description" Binding="{Binding Description, Mode=OneWay}" Width="5*" />
</DataGrid.Columns>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5" Grid.Row="1" >
<Button x:Name="btnClose" Content="Close" Margin="5" Width="50" />
</StackPanel>
</DataGrid>
</Grid>

Is it possible multiple filters in one collection view source wpf

I know one filter in one collection view source but when i use multiple filters in one collection source.Last filter only properly works.Please help me to do multiple filters in one collection view source.
Xaml
<Grid>
<DockPanel>
<DockPanel DockPanel.Dock="Top" Height="90">
<TextBlock VerticalAlignment="Center" Margin="0,9" DockPanel.Dock="Left" >Threshold</TextBlock>
<Slider x:Name="Confidencethreshold" Value="14" Margin="0,5" DockPanel.Dock="Left" Width="200"
Minimum="0" Maximum="50"
ValueChanged="Confidencethreshold_ValueChanged_1"
TickPlacement="BottomRight" TickFrequency="5" IsSnapToTickEnabled="False" SmallChange="1"></Slider>
<StackPanel Orientation="Horizontal" >
<RadioButton x:Name="radioFound" IsChecked="True" Content="Matches" Checked="radioFound_Checked_1" Margin="6" />
<RadioButton x:Name="radioALL" Content="ALL" Checked="radioFound_Checked_1" Margin="6"/>
</StackPanel>
</DockPanel>
<DataGrid Name="DG2" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserSortColumns="True">
<DataGrid.Columns>
<!--<mui:DataGridCheckBoxColumn Header="Silence Reduction" Binding="{Binding P1}"/>-->
<mui:DataGridTextColumn Header="Segment" Binding="{Binding sname}" IsReadOnly="True" />
<!--<mui:DataGridCheckBoxColumn Header="Trimming" Binding="{Binding trim}" />
<mui:DataGridCheckBoxColumn Header="Format Conversion" Binding="{Binding alaw}"/>
<mui:DataGridCheckBoxColumn Header="Recognition" Binding="{Binding recog}" />-->
<!--<mui:DataGridComboBoxColumn Header="Status" SelectedItemBinding="{Binding Status}" ItemsSource="{Binding Source={StaticResource myEnum}}" />-->
<mui:DataGridTextColumn Header="Key" Binding="{Binding key}" IsReadOnly="True" />
<!--<mui:DataGridTextColumn Header="Confidence" Binding="{Binding conf}" IsReadOnly="True" SortDirection="Ascending" SortMemberPath="{Binding conf}" />-->
<mui:DataGridTextColumn Header="Confidence" Binding="{Binding conf}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Grid>
And in c#
MainWindow w1 = (MainWindow)Window.GetWindow(this);
cvs = new CollectionViewSource();
cvs.Source = w1.allresults;
cvs.Filter += cvs_Filter;
DG2.DataContext = cvs;
void cvs_Filter(object sender, FilterEventArgs e)
{
KeysFound t = e.Item as KeysFound;
if (t != null)
// If filter is turned on, filter completed items.
{
if (t.conf < Confidencethreshold.Value)
e.Accepted = false;
else
{
e.Accepted = true;
}
}
}
You can use ICollectionView instead of CollectionViewSource of datagrid source. below link may help..
http://social.msdn.microsoft.com/Forums/vstudio/en-US/82cfac7c-d1f6-421c-8891-1149a90c0dd9/wpf-datagrid-one-itemssource-multiple-views-multiple-filters?forum=wpf

Categories

Resources