How to pass GridView as a ConverterParameter

c#, data-binding, wpf, xaml

Solution

From .NET 4 onwards you could use `x:Reference` which allows you to avoid a `ElementName` binding which can only be set on dependency properties while achieving pretty much the same thing.

Due to cyclical dependency restrictions you cannot reference a control inside itself or its ancestors in the tree. You can however move the binding up a level and just inherit the `DataContext`, e.g.

<Border DataContext="{Binding Path=DisplayDT[0],
                              Converter={StaticResource CNVToColumn},
                              ConverterParameter={x:Reference SeqDtStDataListView1}}">
    <ListView Name="SeqDtStDataListView1" Grid.Row="1">

Problem

I am trying to pass the ListView or the GridView as a ConverterParameter However, in the Converter routine the parameter is coming as a type string Below is the part of the XAML List view and the Converter class. Any help greatly appreciated. Thanks!!! ``` <ListView Name="SeqDtStDataListView1" Grid.Row="1" DataContext="{Binding Path=DisplayDT[0], Converter ={StaticResource CNVToColumn},ConverterParameter=?????}" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionChanged="SEQDatalistview_SelectionChanged" Margin="5"> <ListView.View > <GridView x:Name="SeqDtStDataGridView1"/> </ListView.View> </ListView> ``` Converter: ``` namespace MFTest.Converters { public class CNVToColumn : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DataTable dt = (DataTable)value; GridView GV = (GridView)parameter; <========= fail here =========== if (dt != null && GV != null) foreach (var colum in dt.Columns) // Binding the Columns { DataColumn dc = (DataColumn)colum; GridViewColumn column = new GridViewColumn(); column.DisplayMemberBinding = new Binding(dc.ColumnName); column.Header = dc.ColumnName; GV.Columns.Add(column); } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } ```

Original source