我试图将ListView或GridView作为ConverterParameter传递
但是,在转换器例程中,该参数作为一个类型字符串
但是,在转换器例程中,该参数作为一个类型字符串
以下是XAML列表视图和Converter类的一部分.
任何帮助非常感谢.谢谢!!!
<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>
转换器:
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,System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
解决方法
从.NET 4开始,您可以使用
x:Reference
,它允许您避免只能在依赖属性上设置的ElementName绑定,同时实现几乎相同的事情.
由于循环依赖性限制,您无法引用其本身或其树中的祖先的控件.然而,您可以将绑定移动一级,并继承DataContext,例如.
<Border DataContext="{Binding Path=DisplayDT[0],Converter={StaticResource CNVToColumn},ConverterParameter={x:Reference SeqDtStDataListView1}}"> <ListView Name="SeqDtStDataListView1" Grid.Row="1">