views:

43

answers:

0

Hello,

I have an MVVM application, with a view containing two custom controls, each of which has an ItemsControl that displays the same information in different ways.

Part of the data in the ViewModel is an ObservableCollection, although it is never bound to directly:

ObservableCollection<MyObservableItem> mIncludedCollection;

In the ViewModel, I need to do some computations on the data using another library that takes a List as an argument. For some reason, when I do this line:

List<MyObservableItem> includedList = mIncludedCollection.ToList();

I get some binding errors:

System.Windows.Data Warning: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MyViewOne', AncestorLevel='1''. BindingExpression:Path=DataContext.SomeProperty; DataItem=null; target element is 'Border' (Name='brdOutside'); target property is 'Visibility' (type 'Visibility')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MyViewOne', AncestorLevel='1''. BindingExpression:Path=DataContext.AnotherProperty; DataItem=null; target element is 'Grid' (Name='gridContent'); target property is 'NoTarget' (type 'Object')

*EDIT 1: Adding XAML (leaving out alignment, etc.) *

From main control, with DataContext set to ViewModel in code-behind:

<Grid x:Name="gridMain">
        <local:ViewOne x:Name="viewOne"/>

        <local:ViewTwo x:Name="viewTwo" />
</Grid>

ViewOne.xaml:

<local:ViewOne>
    <!-- DataCollection is an ObservableCollection, but not the same as the collection that complains of binding issues -->
    <ItemsControl x:Name="myPresentation" 
                  ItemsSource="{Binding Source=MyCollectionSource.View}">
        <ItemsControl.ItemsPanel>
           <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"/>
           </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border x:Name="brdOutside"
                        MinHeight="{Binding FallbackValue=0,
                                            Converter={StaticResource ObjectToHeightConverter},
                                                ConverterParameter=204}"
                        MinWidth="{Binding FallbackValue=0,
                                               Converter={StaticResource ObjectToWidthConverter},
                                               ConverterParameter=204}"
                        Style="{StaticResource SomeImageStyle}">

                    <Border.Visibility>
                        <MultiBinding Converter="{StaticResource SomePropertyToVisibility}" FallbackValue="Visible">
                            <Binding Path="DataContext.SomeProperty"
                                     RelativeSource="{RelativeSource FindAncestor, 
                                             AncestorType={x:Type local:ViewOne}}" />
                            <Binding Path="IsSelected" />
                        </MultiBinding>
                    </Border.Visibility>


                    <Grid x:Name="gridContent">
                        <Grid.Resources>
                            <!-- Clear our global tooltip style -->
                            <Style TargetType="{x:Type ToolTip}" />
                        </Grid.Resources>
                        <Grid.Style>
                            <Style TargetType="{x:Type Grid}">
                                <Setter Property="ToolTipService.InitialShowDelay" Value="750" />
                                <Setter Property="ToolTip" Value="{StaticResource ttRegularVersion}" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=DataContext.AnotherProperty, Mode=OneWay,                                                                       
                                                                   RelativeSource={RelativeSource FindAncestor,
                                                                                       AncestorType={x:Type local:ViewOne}}}" 
                                                Value="True">
                                        <Setter Property="ToolTip" Value="{StaticResource ttAlternateView}" />
                                        <Setter Property="ToolTipService.InitialShowDelay" Value="500" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>

                        <local:ViewOneContentWithAppropriateBindings />
                    </Grid>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</local:ViewOne>

ViewTwo.xaml:

<local:ViewTwo>
    <ItemsControl x:Name="myPresentation"
                  ItemsSource="{Binding Source=MyCollectionSource.View}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True"
                           Orientation="Vertical" 
                           VerticalAlignment="Top" />
            </ItemsPanelTemplate>
        <ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate x:Name="itemTemplateDataTemplate">
                <Border x:Name="brdOutside"
                        Height="{Binding Converter={StaticResource ObjectToHeightConverter},
                                    ConverterParameter=204}"
                        Width="{Binding Converter={StaticResource ObjectToWidthConverter},
                                   ConverterParameter=204}">
                    <Border.Visibility>
                        <MultiBinding Converter="{StaticResource SomePropertyToVisibility}">
                            <Binding Path="DataContext.SomeProperty"
                                     FallbackValue="True"
                                         RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type local:ViewTwo}}" />
                            <Binding Path="IsSelected" FallbackValue="True" />
                        </MultiBinding>
                    </Border.Visibility>


                    <Grid x:Name="gridContent">
                        <Grid.Resources>
                            <!-- Clear our global tooltip style -->
                            <Style TargetType="{x:Type ToolTip}" />
                        </Grid.Resources>
                        <Grid.Style>
                            <Style TargetType="{x:Type Grid}">
                                <Setter Property="ToolTipService.InitialShowDelay" Value="500" />
                                <Setter Property="ToolTip" Value="{StaticResource ttRegularMode}" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=DataContext.AnotherProperty, Mode=OneWay,
                                                                   RelativeSource={RelativeSource FindAncestor,
                                                                                       AncestorType={x:Type local:ViewTwo}}}" 
                                                 Value="True">
                                        <Setter Property="ToolTip" Value="{StaticResource ttAlternateView}" />
                                        <Setter Property="ToolTipService.InitialShowDelay" Value="500" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Style>

                        <local:ViewTwoContentWithAppropriateBindings />
                    </Grid>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

END EDIT 1 I am not sure why these binding errors occur. I only get the binding error on one control, even though I have two controls bound (each containing an ItemsControl) to the same data.

Eventually, the binding seems to be resolved, as the data is displayed correctly.

So, why would converting a non-bound ObservableCollection to a List cause a binding issue?

Thanks, wTs