如何将WPF控件的内容绑定到其容器的DataContext,以便我可以基于该对象应用DataTemplateSelector?

时间:2022-03-06 21:04:08

I'm trying to bind a WPF window atop a ViewModel that contains two collections, A and B. I'm attempting to use DataTemplates to display either A or B depending on the setting of a flag in my ViewModel.

我正在尝试在包含两个集合A和B的ViewModel上绑定一个WPF窗口。我正在尝试使用DataTemplates来显示A或B,具体取决于我的ViewModel中标志的设置。

To that end, I've set the window's DataContext = ViewModel. However, when I attempt to bind a ContentControl to that DataContext and apply a DataTemplateSelector to it, the item parameter of the selector's SelectTemplate(object item, DependencyObject container) method is always null:

为此,我设置了窗口的DataContext = ViewModel。但是,当我尝试将ContentControl绑定到该DataContext并对其应用DataTemplateSelector时,选择器的SelectTemplate(对象项,DependencyObject容器)方法的item参数始终为null:

<Window [snip] Title="MainWindow">
    <Window.Resources>
        <!-- DataTemplate and Selector declarations -->
    </Window.Resources>
    <Grid>
        <ContentControl Content="{Binding}"              
                        ContentTemplateSelector="{StaticResource templateSelector}" />
    </Grid>    
</Window>

How should I be binding that ContentControl such that the Window's ViewModel will be passed through to its DataTemplateSelector?

我应该如何绑定ContentControl,以便将Window的ViewModel传递给它的DataTemplateSelector?

2 个解决方案

#1


4  

this worked for me:

这对我有用:

<ContentControl Content="{Binding DataContext, RelativeSource={RelativeSource Self}}"              
                    ContentTemplateSelector="{StaticResource templateSelector}" />

Edit:

I agree with Aaron though, that this might not be the best way to accomplish things. You said you're using a ViewModel. The easiest way would probably be to bind your ItemsControl to a new "SelectedCollection" property on your Viewmodel that exposes the wanted collection. Then in your flag (assuming it is a property) you can fire propertychanged for "SelectedCollection".

我同意Aaron,这可能不是完成任务的最佳方式。你说你正在使用ViewModel。最简单的方法可能是将ItemsControl绑定到Viewmodel上的新“SelectedCollection”属性,该属性公开所需的集合。然后在你的旗帜中(假设它是一个属性)你可以为“SelectedCollection”触发propertychanged。

#2


2  

Lots of things going on here...

这里发生了很多事......

You said you are using the DataTemplateSelector to either display collection A or collection B, while at the same time you stated you are setting one of the collections as the DataContext of the Window.

您说您正在使用DataTemplateSelector来显示集合A或集合B,同时您声明要将其中一个集合设置为Window的DataContext。

If you want to hide the data in one collection perform filtering on the collection itself. Another approach is to run the binding through an IValueConverter or IMultiValueConverter. Yet another solution could be to have two UI elements bound to each collection respectively and change the Visiblity of the UI element based on the value in your ViewModel.

如果要在一个集合中隐藏数据,请对集合本身执行过滤。另一种方法是通过IValueConverter或IMultiValueConverter运行绑定。另一种解决方案可能是分别将两个UI元素绑定到每个集合,并根据ViewModel中的值更改UI元素的Visiblity。

Lot's of different options...and if I understand you correctly the DataTemplateSelector should not be one of them.

很多不同的选项...如果我理解正确,DataTemplateSelector不应该是其中之一。

#1


4  

this worked for me:

这对我有用:

<ContentControl Content="{Binding DataContext, RelativeSource={RelativeSource Self}}"              
                    ContentTemplateSelector="{StaticResource templateSelector}" />

Edit:

I agree with Aaron though, that this might not be the best way to accomplish things. You said you're using a ViewModel. The easiest way would probably be to bind your ItemsControl to a new "SelectedCollection" property on your Viewmodel that exposes the wanted collection. Then in your flag (assuming it is a property) you can fire propertychanged for "SelectedCollection".

我同意Aaron,这可能不是完成任务的最佳方式。你说你正在使用ViewModel。最简单的方法可能是将ItemsControl绑定到Viewmodel上的新“SelectedCollection”属性,该属性公开所需的集合。然后在你的旗帜中(假设它是一个属性)你可以为“SelectedCollection”触发propertychanged。

#2


2  

Lots of things going on here...

这里发生了很多事......

You said you are using the DataTemplateSelector to either display collection A or collection B, while at the same time you stated you are setting one of the collections as the DataContext of the Window.

您说您正在使用DataTemplateSelector来显示集合A或集合B,同时您声明要将其中一个集合设置为Window的DataContext。

If you want to hide the data in one collection perform filtering on the collection itself. Another approach is to run the binding through an IValueConverter or IMultiValueConverter. Yet another solution could be to have two UI elements bound to each collection respectively and change the Visiblity of the UI element based on the value in your ViewModel.

如果要在一个集合中隐藏数据,请对集合本身执行过滤。另一种方法是通过IValueConverter或IMultiValueConverter运行绑定。另一种解决方案可能是分别将两个UI元素绑定到每个集合,并根据ViewModel中的值更改UI元素的Visiblity。

Lot's of different options...and if I understand you correctly the DataTemplateSelector should not be one of them.

很多不同的选项...如果我理解正确,DataTemplateSelector不应该是其中之一。