2
votes

In WPF, I've got a ListView who's ItemSource is bound to an ObservableCollection:

<ListView ItemsSource="{Binding Path=TestList, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

TestList is a collection of type TestCase, which has several members. This works great for populating the ListView columns with info from the TestList members, but I also need to access properties from outside the collection.

There's a ComboBox (outside the ListView) that changes what controls are shown in certain columns. I tried to do this with setting a DataTrigger on the Visibility property:

<GridViewColumn Header="Area" Width="100">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Area}">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Setters>
                            <Setter Property="Visibility" Value="Visible"/>
                        </Style.Setters>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=IdentitySelection}" Value="Test Management">
                                <Setter Property="Visibility" Value="Collapsed"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

I've also got a few other spots where I need bindings from outside TestList (such as populating ComboBoxes within CellTemplates). However, the ListView doesn't seem to be getting data from anything outside the ItemSource.

I tried moving TestList and IdentitySelection into one class (TestManager), declaring that as one, large property, and binding to that:

<ListView ItemsSource="{Binding Path=TestManager, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Area" Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=TestList.Area}">
                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Style.Setters>
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </Style.Setters>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=IdentitySelection}" Value="Test Management">
                                            <Setter Property="Visibility" Value="Collapsed"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

That also did not work. Is there something I need to change about data context? I feel like there must be a way to do this. Any ideas are greatly appreciated.

1

1 Answers

6
votes

Once you are in the DataTemplate, the DataContext switches to the bound item. No getting around it.

However, there are other bindings you can do. For example, if you need something off the main data context, you can do:

"{Binding ElementName=Root, Path=DataContext.MyProperty}"

Note your window or root element needs x:Name="Root" for that to work. You can also still get to resources via StaticResource bindings.