简体   繁体   English

WPF:在ListView的ObservableCollection外部进行绑定

[英]WPF: Binding Outside the ListView's ObservableCollection

In WPF, I've got a ListView who's ItemSource is bound to an ObservableCollection: 在WPF中,我有一个ListView,其ItemSource绑定到ObservableCollection:

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

TestList is a collection of type TestCase, which has several members. TestList是TestCase类型的集合,它具有多个成员。 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. 这对于用TestList成员的信息填充ListView列非常有用,但是我还需要从集合外部访问属性。

There's a ComboBox (outside the ListView) that changes what controls are shown in certain columns. 有一个ComboBox(在ListView之外),用于更改某些列中显示的控件。 I tried to do this with setting a DataTrigger on the Visibility property: 我试图通过在Visibility属性上设置DataTrigger来做到这一点:

<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). 我还有其他一些地方需要从TestList外部进行绑定(例如在CellTemplates中填充ComboBox)。 However, the ListView doesn't seem to be getting data from anything outside the ItemSource. 但是,ListView似乎没有从ItemSource之外的任何事物获取数据。

I tried moving TestList and IdentitySelection into one class (TestManager), declaring that as one, large property, and binding to that: 我尝试将TestList和IdentitySelection移到一个类(TestManager)中,将其声明为一个大属性,并绑定到该类:

<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. 任何想法都将不胜感激。

Once you are in the DataTemplate , the DataContext switches to the bound item. 一旦进入DataTemplateDataContext就会切换到绑定的项目。 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. 请注意,您的窗口或根元素需要x:Name="Root"才能正常工作。 You can also still get to resources via StaticResource bindings. 您仍然仍然可以通过StaticResource绑定访问资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM