Constraint with properties in passing as reference

Hello friends,

I happened to encounter this trouble while trying to pass the reference of a Property.

public class LinkedListQ : QNode

{

private QNode head = null;

public QNode Head

{

get

{

return head;

}

}

class Program

{

static void Main(string[] args)

{

LinkedListQ que = new LinkedListQ();

que.ReverseList(ref que.Head); //Error “A property, indexer or dynamic member access may not be passed as an out or ref parameter”

}

The reason as per MSDN is – Properties are not variables. They are methods, and cannot be passed to ref parameters.

The workaround I figured out is to change the head variable to internal in order to be accessible from the Program class and pass the head variable reference instead of the Property.

internal QNode head = null;

que.ReverseList(ref que.head); //Works fine

Hope you liked the post. Please share your comments.
Reference: C# Reference

Remove Duplicates in Dataset Efficiently

Hello guys,

Do you remember the pain of removing duplicates in fetched dataset by looping, comparing etc.

it can be handled easily by using DataView.ToTable Method. The syntax is below.

DataView.ToTable(bool distinct, string[] columnNames)

distinct: If it’s true, the returned DataTable contains rows that have distinct values for all its columns specified in the second parameter. Default value is false.

columnNames: A string array that contains a list of the column names to be included in the returned table. The order of columns in returned table would be same as it’s appear in the array.

Ex1

DataTable temp = dt.DefaultView.ToTable(true, “Region”);

Ex2

DataTable temp = dt.DefaultView.ToTable(true, “Region”, “City”);

Hope you liked the post. Please share your comments.

Reference:

DataView.ToTable Method

Data Grid Best Practices in WPF

Hello friends,

While working on WPF, I came to know some good Data grid tips and wanted to share you the same.

Issue

WPF Data Grid is not reflecting the changed Item Source.

Solution

If using MVVM pattern, Refresh the item source by below command.

CollectionViewSource.GetDefaultView(<Your Data Grid collection name>).Refresh();

Issue

How to open a context menu from data grid and assign event handler after clicking on menu item.

Solution

Step A: create context menu by inserting the following code.

<Window.Resources>

<ContextMenu x:Key=”mnuAddList” Name=”mnuAddList”>

<MenuItem Name=”mnuAdd” Header=”Edit” Command=”{Binding PopUpAddCommand}”>

</MenuItem>

</ContextMenu>

</Window.Resources>

Please note that here Context menu key (i.e. mnuAddList) will be used to attach with data grid in the next step. Also PopUpAddCommand is a property (defined in View Model) of DelegateCommand type which will have reference of event handler.

Step B: Attach the menu created in step 1 with grid.

<DataGrid Name=”grdAdd” ContextMenu =”{StaticResource mnuAddList}” >

Issue

Visibility property is not setting for Grid column and by doing so getting error as “System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsMyColumnVisibile; DataItem=null; target element is’DataGridTextColumn’ (HashCode=7395880); target property is ‘Visibility’ (type’Visibility’)”

Solution

DataGridColumns are not part of visual tree so they are not connected to the data context of the DataGrid due to that their visibility binding can’t be done straight forward. To achieve this, following steps needs to performed.

  • Add a proxy FrameworkElement in your ancestor panel’s Resources.
  • Host it into an invisible ContentControl bound to its Content.
  • Use this ProxyElement as StaticResource for data context source in your visibility binding.

Code snippet

<StackPanel>

<StackPanel.Resources>

<BooleanToVisibilityConverter x:Key=”BooleanToVisibilityConverter” />

<FrameworkElement x:Key=”ProxyElement” DataContext=”{Binding}”/>

</StackPanel.Resources>

<ContentControl Visibility=”Collapsed” Content=”{StaticResource ProxyElement}”/>

<DataGrid AutoGenerateColumns=”False”>

<DataGrid.Columns>

<DataGridTextColumn Visibility=”{Binding DataContext.IsMyColumnVisibile, Source={StaticResource ProxyElement}, Converter={StaticResource BooleanToVisibilityConverter}}” Binding=”{Binding MyColumn}”/>

</DataGrid.Columns>

</DataGrid>

</StackPanel>

Hope you liked the post. Please share your comments.