【问题标题】:Binding in Style Setter in DataTemplate WPFDataTemplate WPF 中样式设置器中的绑定
【发布时间】:2017-01-30 01:28:24
【问题描述】:

好的,我有一个相对复杂的问题。我正在尝试在 WPF 中创建一个窗口。此窗口上的主要元素是 DataGrid。 DataGrid 中的每一行都有一个我使用DataGrid.RowDetailsTemplate 设置的DetailsPane。根据某些特定于行的值,我需要 DetailsPane 来显示不同的元素。为此,我在 DataTemplate 的根目录放置了一个 ContentControl,并使用带有 DataTriggers 的 Style 来设置它的 Content 属性。现在,inside 这些 Setter 之一是 ComboBox。此 ComboBox 需要将其 ItemsSource 绑定到一个列表,该列表存储在 Window 级别的依赖项属性中(因为无论行如何,它都是同一个列表)。以下是我正在查看的简化版本:

<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>

所以我想弄清楚的是如何将该 ComboBox 的 ItemsSource 绑定到顶级窗口的依赖项属性。 Andy 知道如何实现这一点?

编辑:

我之前应该提到过这一点,但我已经尝试在绑定中使用 {RelativeSource AncestorType=Window} 和 ElementName。在这两种情况下,ComboBox 中的列表在运行时都是空白的。

【问题讨论】:

标签: wpf xaml data-binding datatemplate wpf-style


【解决方案1】:
ItemsSource="{Binding WhateverList, RelativeSource={RelativeSource AncestorType=Window}}"

【讨论】:

  • 对不起,我已经试过了。我应该把它放在问题中,我更新了它。
猜你喜欢
  • 1970-01-01
  • 2020-12-15
  • 2012-04-11
  • 1970-01-01
  • 2016-01-11
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
相关资源
最近更新 更多