Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass GridView as a ConverterParameter

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();
    }

}
}
like image 556
Irmi Mrm Avatar asked Apr 11 '12 12:04

Irmi Mrm


2 Answers

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">
like image 175
H.B. Avatar answered Oct 06 '22 16:10

H.B.


You can't bind to it.

ConvertParameter inherits from Object and therefore is not bindable.

You can, however, define your Binding in the code behind instead of doing it in the XAML part.

System.Windows.Data.Binding b = new System.Windows.Data.Binding();
b.ConverterParameter = this;

Please read about the limitations on the ConvertParameter here

like image 32
Luis Filipe Avatar answered Oct 06 '22 17:10

Luis Filipe