简体   繁体   English

在DataTemplate WPF中的样式设置器中进行绑定

[英]Binding in Style Setter in DataTemplate WPF

Okay, I have a relatively involved problem. 好的,我有一个相对复杂的问题。 I'm trying to create a Window in WPF. 我正在尝试在WPF中创建一个窗口。 The main element on this window is a DataGrid. 此窗口上的主要元素是DataGrid。 Each one of the rows in the DataGrid has a DetailsPane which I set using DataGrid.RowDetailsTemplate. DataGrid中的每一行都有一个DetailsPane,我使用DataGrid.RowDetailsTemplate进行了设置。 Depending on certain row-specific values, I need the DetailsPane to display different elements. 根据某些特定于行的值,我需要DetailsPane显示不同的元素。 To accomplish this I placed a ContentControl at the root of the DataTemplate and used a Style with DataTriggers to set its Content property. 为此,我在DataTemplate的根目录下放置了一个ContentControl,并使用了带有DataTriggers的Style来设置其Content属性。 Now, inside one of these Setters is a ComboBox. 现在, 这些Setter之一中是一个ComboBox。 This ComboBox needs to have its ItemsSource bound to a list, which is stored in a dependency property on the Window level (because its the same list regardless of the row). 此ComboBox需要将其ItemsSource绑定到一个列表,该列表存储在Window级别的依赖项属性中(因为无论列表的行如何,它的列表都是相同的)。 Below is a simplified version of what I'm looking at: 以下是我正在查看的内容的简化版本:

<Window>
    ...
    <DataGrid>
        ...
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <ContentControl>
                    <Style TargetType="ContentControl">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RowSpecificBooleanProperty}" Value="False">
                                <Setter Property="Content">
                                    <Setter.Value>
                                        ...
                                        <ComboBox ItemsSource={HowDoIBindThisToTheWindowProperty}/>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
</Window>

So what I'm trying to figure out is how to bind the ItemsSource of that ComboBox to a dependency property of the top-level window. 所以我想弄清楚的是如何将ComboBox的ItemsSource绑定到顶层窗口的依赖项属性。 Andy idea how to accomplish that? 安迪的想法是如何做到的?

EDIT: 编辑:

I should have mentioned this before but I've already tried using {RelativeSource AncestorType=Window} and ElementName in the binding. 我之前应该提到过,但是我已经尝试在绑定中使用{RelativeSource AncestorType = Window}和ElementName。 In both cases the list in the ComboBox is blank at runtime. 在这两种情况下,ComboBox中的列表在运行时都是空白的。

ItemsSource="{Binding WhateverList, RelativeSource={RelativeSource AncestorType=Window}}"

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

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