ListView.View is null when used in a DataTemplate

I would like to create a WPF UserControl that can display data in different layouts, in the main table, group box and tabbed tab. I would like the control to be such that it can accept recursively. For example, I would like to display a table with a group box in one of my cells and again split the table inside the group box.

For this, I used TemplateSelector ("GenericLayoutTemplateSelector") as a top-level element in xaml with different templates. For the layout of the table, I wanted to use the example โ€œLinking ListView with Data Matrixโ€ in the code project: http://www.codeproject.com/Articles/36462/Binding-a-ListView-to-a-Data-Matrix

The codeproject example works fine, but when I use it in my context, it fails. Since the only change I made was put in xaml inside the datatemplate / template selector, I think the problem could be related to this.

<DataTemplate x:Key="TableTemplate"> <Border BorderThickness="2" BorderBrush="SteelBlue"> <ListView x:Name="TableLayoutListView" tableLayout:ListViewExtension.MatrixSource="{Binding Converter={StaticResource ListToMatrixConverter}}"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Path=[0]}" Header="Dummy" CellTemplateSelector="{DynamicResource GenericLayoutTemplateSelector}"/> </GridView> </ListView.View> </ListView> </Border> </DataTemplate> 

This fails at the point where the GridView is retrieved inside the ListViewExtension.MatrixSource:

 private static void OnMatrixSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ListView listView = d as ListView; Matrix matrix = e.NewValue as Matrix; listView.ItemsSource = matrix; GridView gridView = listView.View as GridView; DataTemplateSelector cellTemplateSelector = gridView.Columns.First().CellTemplateSelector; 

ListView.View is null. I can create a GridView in the code and assign it to listView.View, but then I do not have access to the GenericTemplateSelector, which I wanted to use CellTemplateSelector for the GridView.

Any ideas why listView.View is null, what can I do about it?

Edit: SledgeHammer made this obvious, what I'm trying to do is a bad hack, and it failed. The proper way to do this is to pass the GenericTemplateSelector directly to the attached property, and not through the dummy GridView table.

I am trying to figure out a way to do this: my idea is to have an attached property with a multiply connected interface.

 <DataTemplate x:Key="TableTemplate"> <Border BorderThickness="2" BorderBrush="SteelBlue"> <ListView x:Name="TableLayoutListView"> <tableLayout:ListViewExtension.MatrixSourceWithTemplateSelector> <MultiBinding Converter={StaticResource ListToMatrixConverter}> <Binding Path="this" /> <Binding {DynamicResource GenericLayoutTemplateSelector}/> </MultiBinding> </tableLayout:ListViewExtension.MatrixSourceWithTemplateSelector> 

However, this is not a valid haml. Any ideas how to get {DynamicResource GenericLayoutTemplateSelector} in a multi-connected?

+4
source share
1 answer

I need to guess that the MatrixSource property is set before the View property. Obviously, they cannot be installed at the same time. Refer to the matrix source and the "View" changes and call the general method and check the zero value.

0
source

All Articles