views:

17

answers:

1

Hi,

I'm pretty new to XAML, and my first task in a new job is to untangle a particularly malodorous collection of spaghetti code. Learning XAML at the same time as untangling this horror-show is proving somewhat beyond me, so my apologies for the homework-level questions I'm asking, these days.

Anyway, I have the following XAML code:

<UserControl.Resources>
    <CollectionViewSource x:Key="XMLObjectGroups" Source="{Binding Path=XMLObjectList}">                              
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="IsDateType"/>
        </CollectionViewSource.GroupDescriptions>
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="IsDateType" Direction="Ascending"/>
            <scm:SortDescription PropertyName="OrderNumber" Direction="Ascending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
<UserControl.Resources>
...
    <ListView 
          DataContext="{StaticResource XMLObjectGroups}"
          ItemContainerStyle="{StaticResource XMLItemStyle}"
          ItemsSource="{Binding}"
          SelectedItem="{Binding Path=SelectedOrder}">

On the second line XMLObjectList is a readonly property of a ViewModel class, returning a Collections.ObjectModel.ReadOnlyObservableCollection(Of MyOrder).

On the final line SelectedOrder is a property of the same ViewModel class, which allows setting and getting of a MyOrder object.

I have confirmed that XMLObjectList is being correctly referenced by renaming the property, mistyping the string, breakpoints, etc. XMLObjectList definitely references the XMLObjectList property of this particular ViewModel class.

The SelectedOrder property, however, is never accessed at runtime, meaning that it isn't properly hooked up to the SelectedItem of this ListView.

As far as my reading around the subject goes, and as far as the answers I got to a similar question yesterday, the code I have should be working, but it isn't. What am I doing wrong, here?

+1  A: 

Try changing it to the following:

<ListView  
      ItemContainerStyle="{StaticResource XMLItemStyle}" 
      ItemsSource="{Binding Source={StaticResource XMLObjectGroups}}" 
      SelectedItem="{Binding Path=SelectedOrder}"> 
rudigrobler
Brilliant. That worked! Could you explain what has made the difference?
Frosty840
In your original code you set the DataContext explicitly! Then when you bind the SelectedItem, it trys to binds to the SelectedOrder property on XMLObjectList (Which do not exist)
rudigrobler