Data Grid filter performance wpf - wpf

I have a DataGrid with textbox in the headers for filter, but the performance is not so good. This is the code for the datagrid:
<DataGrid x:Name="DGrdBuscar" ItemsSource="{Binding DGrdBuscarView,Mode=OneWay}" MaxWidth="1800" MaxHeight="850" MinRowHeight="26" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="6" HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="Auto" AlternatingRowBackground="LightCyan" AlternationCount="2" AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single" ColumnWidth="SizeToCells" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" FrozenColumnCount="1" Margin="0,0,0,10" BorderBrush="Black" VirtualizingStackPanel.VirtualizationMode="Recycling">
<DataGrid.Resources>
<local:BindingProxy x:Key="Proxy" Data="{Binding}"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="IdTrafo" Binding="{Binding IdTrafo,Mode=OneWay}" Width="Auto" MinWidth="130" CellStyle="{StaticResource DataGridCellStyleLeft}" HeaderStyle="{StaticResource DataGridColHeaderStyle}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center"/>
<TextBox x:Name="TxtIdTrafo" TextChanged="Txt_TextChanged" Style="{StaticResource TextBoxSmallStyle}" FontWeight="Bold" MinWidth="150"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridCheckBoxColumn Header="{x:Static lang:Resources.Buscar_Def}" Binding="{Binding Definitivo,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}" CanUserResize="False"/>
<DataGridCheckBoxColumn Header="{x:Static lang:Resources.NroDiseno_Cot}" Binding="{Binding Cotizacion,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}" CanUserResize="False"/>
<DataGridTextColumn Header="{x:Static lang:Resources.Buscar_UserName}" Binding="{Binding UserName,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center"/>
<TextBox x:Name="TxtUserName" TextChanged="Txt_TextChanged" Style="{StaticResource TextBoxSmallStyle}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn x:Name="colPotReg1" Header="{x:Static lang:Resources.Buscar_PotReg1}" Binding="{Binding PotReg1,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center"/>
<TextBox x:Name="TxtPotReg1" TextChanged="Txt_TextChanged" Style="{StaticResource TextBoxSmallStyle}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="{x:Static lang:Resources.Buscar_UnomAT}" Binding="{Binding UnomAT,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center"/>
<TextBox x:Name="TxtUnomAT" TextChanged="Txt_TextChanged" Style="{StaticResource TextBoxSmallStyle}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="{x:Static lang:Resources.Buscar_UnomBT}" Binding="{Binding UnomBT,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center"/>
<TextBox x:Name="TxtUnomBT" TextChanged="Txt_TextChanged" Style="{StaticResource TextBoxSmallStyle}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="{x:Static lang:Resources.Buscar_Frec}" Binding="{Binding Frecuencia,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center"/>
<TextBox x:Name="TxtFrec" TextChanged="Txt_TextChanged" Style="{StaticResource TextBoxSmallStyle}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="{x:Static lang:Resources.Observaciones}" Binding="{Binding ObservacionesSearch,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}" HorizontalAlignment="Center"/>
<TextBox x:Name="TxtObs" TextChanged="Txt_TextChanged" Style="{StaticResource TextBoxSmallStyle}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Header="{x:Static lang:Resources.Buscar_GrupoCon}" Binding="{Binding GrupoCon,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}" Visibility="{Binding Data.GrupoCon_IsCollapsed, Converter={StaticResource BoolToVisConverter}, Source={StaticResource Proxy}}"/>
<DataGridTextColumn Header="{x:Static lang:Resources.Buscar_NroDiseno}" Binding="{Binding NroDiseno,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}"/>
<DataGridTextColumn Header="{x:Static lang:Resources.Buscar_NroSerie}" Binding="{Binding NroSerie,Mode=OneWay}" Width="Auto" CellStyle="{StaticResource DataGridCellStyle}" HeaderStyle="{StaticResource DataGridColHeaderStyle}"/>
</DataGrid.Columns>
</DataGrid>
The datagrid is fill from a database and it can contains thousands of rows.
And in the code behind:
Private Sub Txt_TextChanged(sender As Object, e As TextChangedEventArgs)
Try
Dim t As TextBox = CType(sender, TextBox)
Dim filter As String = t.Text
If filter = "" Then
'v_MainWindowVM.BuscarVM.DGrdBuscarView.Filter = Nothing
Select Case t.Name
Case "TxtPotReg1"
filterPotReg1 = ""
Case "TxtUnomAT"
filterUnomAT = ""
Case "TxtUnomBT"
filterUnomBT = ""
Case "TxtFrec"
filterFrec = ""
Case "TxtUserName"
filterUserName = ""
Case "TxtObs"
filterObs = ""
Case "TxtIdTrafo"
filterIdTrafo = ""
End Select
v_MainWindowVM.BuscarVM.DGrdBuscarView.Filter = New System.Predicate(Of [Object])(Function(o) TryCast(o, ClassDataBase.DataGridBuscar).PotReg1.ToString.StartsWith(filterPotReg1) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).UnomAT.ToString.StartsWith(filterUnomAT) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).UnomBT.ToString.StartsWith(filterUnomBT) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).Frecuencia.ToString.StartsWith(filterFrec) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).UserName.ToUpper.StartsWith(filterUserName.ToUpper) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).ObservacionesSearch.Contains(filterObs) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).IdTrafo.ToUpper.StartsWith(filterIdTrafo.ToUpper))
Else
Select Case t.Name
Case "TxtPotReg1"
filterPotReg1 = t.Text
Case "TxtUnomAT"
filterUnomAT = t.Text
Case "TxtUnomBT"
filterUnomBT = t.Text
Case "TxtFrec"
filterFrec = t.Text
Case "TxtUserName"
filterUserName = t.Text
Case "TxtObs"
filterObs = t.Text
Case "TxtIdTrafo"
filterIdTrafo = t.Text
End Select
v_MainWindowVM.BuscarVM.DGrdBuscarView.Filter = New System.Predicate(Of [Object])(Function(o) TryCast(o, ClassDataBase.DataGridBuscar).PotReg1.ToString.StartsWith(filterPotReg1) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).UnomAT.ToString.StartsWith(filterUnomAT) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).UnomBT.ToString.StartsWith(filterUnomBT) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).Frecuencia.ToString.StartsWith(filterFrec) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).UserName.ToUpper.StartsWith(filterUserName.ToUpper) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).ObservacionesSearch.Contains(filterObs) AndAlso TryCast(o, ClassDataBase.DataGridBuscar).IdTrafo.ToUpper.StartsWith(filterIdTrafo.ToUpper))
End If
Catch ex As Exception
ErrorMsg("Subrutina ITD.Buscar.Txt_TextChanged", ex)
End Try
End Sub
What can I do to improve the performance?
When I start writing in the filter after the second letter it take too long.

I improve the performance by doing the TextChanged method Async, like this:
Private mostRecentfilter As String = ""
Private Async Sub Txt_TextChanged(sender As Object, e As TextChangedEventArgs)
Try
Dim t As TextBox = CType(sender, TextBox)
Dim filter As String = t.Text 'get the entered text
mostRecentfilter = filter 'set the instance variable for entered text
Await Task.Delay(550) 'wait 0.55 second in case they keep typing
'if they didn't keep typing
If filter = mostRecentfilter Then
'do what you were going to do
If filter = "" Then
Select Case t.Name
Case "TxtPotReg1"
filterPotReg1 = ""
Case "TxtUnomAT"
filterUnomAT = ""
Case "TxtUnomBT"
filterUnomBT = ""
Case "TxtFrec"
filterFrec = ""
Case "TxtUserName"
filterUserName = ""
Case "TxtObs"
filterObs = ""
Case "TxtIdTrafo"
filterIdTrafo = ""
End Select
Else
Select Case t.Name
Case "TxtPotReg1"
filterPotReg1 = t.Text
Case "TxtUnomAT"
filterUnomAT = t.Text
Case "TxtUnomBT"
filterUnomBT = t.Text
Case "TxtFrec"
filterFrec = t.Text
Case "TxtUserName"
filterUserName = t.Text
Case "TxtObs"
filterObs = t.Text
Case "TxtIdTrafo"
filterIdTrafo = t.Text
End Select
End If
v_MainWindowVM.BuscarVM.DGrdBuscarView.Filter = New Predicate(Of Object)(Function(o As ClassDataBase.DataGridBuscar) o.IdTrafo.ToUpper.StartsWith(filterIdTrafo.ToUpper) AndAlso o.PotReg1.ToString.StartsWith(filterPotReg1) AndAlso o.UnomAT.ToString.StartsWith(filterUnomAT) AndAlso o.UnomBT.ToString.StartsWith(filterUnomBT) AndAlso o.Frecuencia.ToString.StartsWith(filterFrec) AndAlso o.UserName.ToUpper.StartsWith(filterUserName.ToUpper) AndAlso o.ObservacionesSearch.Contains(filterObs))
End If
Catch ex As Exception
ErrorMsg("Subrutina ITD.Buscar.Txt_TextChanged", ex)
End Try
End Sub
And also removing the trycast.

Related

Why is my DataGrid not showing the values from my list?

So I have 2 DataGrids (because I haven't found a better way to do it the way I want it) and I have the following code in the .cs:
public MainWindow()
{
InitializeComponent();
List<User> users = new List<User>();
users.Add(new User() { Id = 1, Name = "John Doe", Birthday = new DateTime(1971, 7, 23) });
users.Add(new User() { Id = 2, Name = "Jane Doe", Birthday = new DateTime(1974, 1, 17) });
users.Add(new User() { Id = 3, Name = "Sammy Doe", Birthday = new DateTime(1991, 9, 2) });
gridd.ItemsSource = users;
string[] items = new string[]
{
"Asdf",
"qwer",
"sdfg",
"wert",
};
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
I only want to output this data in my DataGrid for testing purposes, but for some reason it doesn't work. Here is my xaml code:
<Grid>
<DataGrid HorizontalAlignment="Left" Height="298.638" Margin="93.195,102.655,0,0"
VerticalAlignment="Top" Width="507.242">
<DataGrid.Resources>
<!--Design kopfzeile-->
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderThickness" Value="0,0,1,0" />
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="Padding" Value="5 0 0 0"/>
</Style>
<!--Deaktivieren Des rowheader-->
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Background" Value="Transparent"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="*" Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Artikelnummer" FontSize="10" IsReadOnly="True"/>
<DataGridTextColumn Width="*" Binding="{x:Null}" ClipboardContentBinding="{x:Null}" FontSize="10" Header="Artikelnummer" IsReadOnly="True"/>
<DataGridTextColumn Width="*" Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Matchcode" IsReadOnly="True"/>
<DataGridTextColumn Width="*" Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="Hersteller" IsReadOnly="True"/>
<DataGridCheckBoxColumn Width="*" Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Header="CheckBox" />
</DataGrid.Columns>
</DataGrid>
<!--!!!!!!Haupt DataGrid !!!!!-->
<DataGrid AutoGenerateColumns="False" Height="282.293" HorizontalAlignment="Left" Margin="93.195,119,0,0" Name="gridd" VerticalAlignment="Top" Width="507.242" >
<DataGrid.Columns >
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="88.048">
<TextBox Name="IDSearcBox" Width="88"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="NameSearchBox" Width="88"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="NameSearchBox" Width="88"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="NameSearchBox" Width="88"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="NameSearchBox" Width="88"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
It shows me the number of lines as I have saved in the list:
But it does not show the values for it and especially since it is formatted in a weird way. I would be happy if someone sees my mistake here.
You use a DataGridTemplateColumn but you do not supply data templates for displaying and editing.
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="NameSearchBox" Width="88"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Alternatively, just use already built-in column types like DataGridTextColumn. In case you do not need custom data templates to display your data, there is no need for a template column.
<DataGridTextColumn Width="*" Binding="{Binding Id}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="88.048">
<TextBox Name="IDSearcBox" Width="88"/>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>

How to work on control inside Datagrid in WPF?

I m stuck in minor issue,which become time consuming as per my project concern,i am using datagrid in my wpf project in which my requirement is to show payment button only when status is "Not Paid",here i had use DataGridTemplateColumn in which i m using Button and on grid only i m getting status of payment.
Here is my xaml File
<DataGrid Name="gridInvoiceList" SelectedItem="{Binding SelectedDetails}" ColumnHeaderHeight="35" ItemsSource="{Binding ItemsSource, ElementName=pageControl, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" CanUserAddRows="false"
HorizontalAlignment="Left" AutoGenerateColumns="False"
Width="978" RowHeaderWidth="20" RowHeight="25"
CanUserSortColumns="False" SelectionChanged="gridInvoiceList_SelectionChanged" Height="220" LoadingRow="gridInvoiceList_LoadingRow" SelectionMode="Extended" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="160" Binding="{Binding FullName }" CanUserSort="True" IsReadOnly="True" />
<DataGridTextColumn Header="Total Amount" Width="150" Binding="{Binding TotalAmount,ConverterCulture='en-GB',StringFormat=-{0:C}}" CanUserSort="True" IsReadOnly="True" />
<DataGridTextColumn Header="Invoice Date" Width="200" Binding="{Binding InvoiceDate}" CanUserSort="True" IsReadOnly="True"/>
<DataGridTextColumn Header="Payment Status" Width="220" Binding="{Binding PaymentStatus}" CanUserSort="True" IsReadOnly="True" />
<DataGridTemplateColumn Header="Pay" Width="105">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Pay" HorizontalAlignment="Center" VerticalAlignment="Top" Name="btnPay" Click="btnPay_Click" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Print" Width="105">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Print" HorizontalAlignment="Center" VerticalAlignment="Top" Name="btnPrintInvoice" Click="btnPrint_Click" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I had use some code from reference code which had not fullfill my requirement
You can use a converter to achieve that. But I'll you show you the simplest, easiest way:
Create a property:
public System.Windows.Visibility PayButtonVisibility
{
get
{
if (PaymentStatus == MyEnum.NotPaid)
return System.Windows.Visibility.Visible;
return System.Windows.Visibility.Collapsed;
}
}
Then bind your button visibility to it:
<Button Content="Pay" HorizontalAlignment="Center" VerticalAlignment="Top" Name="btnPay" Click="btnPay_Click" Visibility="{Binding PayButtonVisibility}" />

Showing Hiding a control inside datagrid in silverlight with MVVM

I have a datagrid like below
**strong text**
<sdk:DataGrid AutoGenerateColumns="False" Height="Auto" MaxHeight="500"
HorizontalAlignment="Left" Margin="5" Name="_EmployeeGrid"
ItemsSource="{Binding Path= Employees,Mode=TwoWay}"
HorizontalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Visibility="Visible"
VerticalAlignment="Top" Width="Auto">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Employee ID" IsReadOnly="True" Binding="{Binding Path=EmpID}"/>
<sdk:DataGridTemplateColumn Header="Name" SortMemberPath="EmpID">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton VerticalAlignment="Center" NavigateUri="{Binding Path=EmpID,
Converter={StaticResource NavigatePropertyUriConverter}, ConverterParameter=EmpView}"
Content= "{Binding Path=EmpID}" >
</HyperlinkButton>
<!--<HyperlinkButton NavigateUri= "{ Binding Converter={StaticResource navigateConv}, ConverterParameter=/Property/IssuesView?}" VerticalAlignment="Center"
Content="{Binding Path=PropertyId}"
HorizontalAlignment="Right" Margin="5"/>-->
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<!--<sdk:DataGridTextColumn Header="Property #" IsReadOnly="True" Binding="{Binding Path=PropertyId}"/>-->
<sdk:DataGridTextColumn Header="Address" IsReadOnly="True" Binding="{Binding Path=Address}"/>
<sdk:DataGridTextColumn Header="Category" IsReadOnly="True" Binding="{Binding Path=CategoryName}"/>
<sdk:DataGridTextColumn Header="Phone" IsReadOnly="True" Binding="{Binding Path=UnitId}" Visibility="Collapsed"/>
<!--<sdk:DataGridTextColumn Header="Notes" Binding="{Binding Comments}" IsReadOnly="False" />-->
<sdk:DataGridTemplateColumn x:Name="Notes1" Header="Notes">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock MaxWidth="200" TextAlignment="Left" TextWrapping= "Wrap" Text="{Binding Path=Comments, Mode=OneWay}" Width="200" />
<HyperlinkButton Name="btnEllipsis" Visibility="{Binding EllipsisVisibility,Source={StaticResource ViewModel}}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="15" Content="..." ToolTipService.ToolTip="Click For More" Command="{Binding NotesCommand,Source={StaticResource ViewModel}}" CommandParameter="{Binding}" >
</HyperlinkButton>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid>
and associated property in viewmodel as below
strong text
string _ellipsisVisibility;
public string EllipsisVisibility
{
get { return _ellipsisVisibility; }
set
{
if (_ellipsisVisibility != value)
{
_ellipsisVisibility = value;
RaisePropertyChanged("EllipsisVisibility");
}
}
}
and setting it to
EllipsisVisibility="Collapsed";
and
EllipsisVisibility="Visible";
There are two questions
1. It is not working to show or hide column
2. Need to show the hyperlink button in few rows and or hide in few rows based on some condition like Rowdatabound event in asp.net.
How to do can anyone please help???
(1) DataGrid column visibility is, unfortunately, not a dependency property. This means it can't engage in databinding. Some work-arounds to this involve subclassing the column type you're interested in.
(2) To show/hide a particular control, based on the data for that row, I would use a converter. Or create a new property on that entity type to directly bind to.

Silverlight WCF databind related tables

I have a table with foreign keys. I want to display the data as the description and not the Id for the related table. From the Entity service I have made the .Include statement on the data model for it to return. Here is what I have tried, the station shows up in the cell, but nothing in the combobox:
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Width="Auto" Header="Station">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Station.Description}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Station}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<OperationContract()>
Public Function LoadPersonnel(clientId As Integer) As List(Of Personnel)
Dim result As List(Of Personnel)
Try
result = db.Personnels.Include("Station").Where(Function(o) o.ClientId = clientId).ToList
Catch
result = Nothing
End Try
Return result
End Function
Also tried:
<ComboBox ItemsSource="{Binding}" DisplayMemberPath="{Binding Station.Description}" SelectedItem="{Binding Station.Description}" SelectedValue="{Binding Station.StationId}" />
Do like this,
<sdk:DataGrid AutoGenerateColumns="False" Height="100" HorizontalAlignment="Left" Margin="107,90,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="251" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn CanUserReorder="False" CanUserResize="False" CanUserSort="False" Width="Auto" Header="Station">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Station.Description}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Station}" DropDownOpened="ComboBox_DropDownOpened" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
in the drop down opened event, assign the itemssource,
private void ComboBox_DropDownOpened(object sender, EventArgs e)
{
var combo = (ComboBox)sender;
combo.ItemsSource = Station;
combo.DisplayMemberPath = "Description";
}

WPF 3.5 nested datagrid row details not showing in Visual Tree

So I build a nice nested datagrid that look something like this:
<dg:DataGrid x:Name="mainGrid"
AutoGenerateColumns="False"
CanUserAddRows="False"
AreRowDetailsFrozen="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
SelectionUnit="FullRow"
RowDetailsVisibilityMode="VisibleWhenSelected">
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="36" IsReadOnly="False">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="+" Width="28" Height="28" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Name" IsReadOnly="True" Width="582">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" VerticalAlignment="Center" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=Default}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Status" IsReadOnly="True" Width="150">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" VerticalAlignment="Center" Text="{Binding Status, Mode=TwoWay, UpdateSourceTrigger=Default}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridCheckBoxColumn Binding="{Binding IsSelected}"/>
</dg:DataGrid.Columns>
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<dg:DataGrid x:Name="scondaryDatagrid" Margin="29,0,0,0"
AutoGenerateColumns="False"
CanUserAddRows="False"
AreRowDetailsFrozen="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
SelectionUnit="Cell"
RowDetailsVisibilityMode="VisibleWhenSelected"
ItemsSource="{Binding Grades, Mode=TwoWay}">
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="32" IsReadOnly="True">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="+" Width="28" Height="28" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="32" IsReadOnly="True">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Name" IsReadOnly="True" Width="550">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" Text="{Binding Name, Mode=TwoWay}" VerticalAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<dg:DataGrid Margin="25,0,0,0"
AutoGenerateColumns="False"
CanUserAddRows="False"
AreRowDetailsFrozen="True"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="False"
SelectionUnit="Cell"
ItemsSource="{Binding Notes, Mode=TwoWay}">
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="32" IsReadOnly="True">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<dg:DataGridTemplateColumn Header="Note" IsReadOnly="True" Width="550">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="5" Text="{Binding Note, Mode=TwoWay}" VerticalAlignment="Center" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
</dg:DataGrid>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
</dg:DataGrid>
Due to the RowDetailsVisibilityMode property when it is set to VisibleWhenSelected, the Node DataGridDetailsPresenter for the rows have 0 children. When the property is set to Visible I can see the DataGridDetailsPresenter children.
How can I force the inclusion of all the nodes in the Visual tree for when the RowDetailsVisibilityMode property is VisibleWhenSelected?
Any input is greatly appreciated.
What are you trying to accomplish?
Unless the the RowDetails are visible they would not be in the Visual Tree unless could force their creation by setting the RowDetailsVisibilityMode to Visible but setting the RowDetails DataGrid visibility to Collapsed. This may not work. I don't know if the DataGrid would be in the Visual Tree if set to Collapsed in 3.5.

Resources