【问题标题】:XAML and Caliburn.Micro MVVM: Style resource referencing another style resource in a data templateXAML 和 Caliburn.Micro MVVM:样式资源引用数据模板中的另一个样式资源
【发布时间】:2015-01-13 00:29:44
【问题描述】:

所以,我在 Visual Studio 中创建了一个 UI,并且由于所有重复的元素,我在我的用户控件的资源部分中使用了很多元素。我要做的是使用文件中的另一个 StaticResource 作为项目的模板来遍历项目。

问题在于Datatemplate 当然会将DataContext 切换到它正在迭代的项目。所以我试图弄清楚如何获取静态资源的上下文,它位于UserControl 的上下文中。我考虑过使用相对采购,但后来意识到StaticResource 没有该选项,只能让您访问上下文的资源。

有什么建议可以在这里做什么?

    <Style TargetType="ItemsControl" x:Key="BeneficiaryList">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Expander Header="{Binding Name_1}">
                        <Expander.Resources>
                            <utils:BindingProxy x:Key="proxy" Data="{Binding}"/>
                        </Expander.Resources>
                        <!--Attempt at a proxy binding to access another style resource in the file. Does not work.-->
                        <ContentControl Style="{StaticResource proxy.Person_Template}"/>
                    </Expander>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

谢谢!

【问题讨论】:

    标签: c# wpf xaml mvvm caliburn.micro


    【解决方案1】:

    您只需给您的 UserControl 一个名称,例如“root”,然后使用ElementName绑定:

    <ContentControl Style="{Binding ElementName=root,Path=DataContext.WhateverProperty}" />
    

    【讨论】:

    • 另一个问题,我如何才能到达&lt;UserControl.Resources&gt; 元素中的Style 元素?是否像使用Path=DataContext.Resources.Person_Template 一样简单?还是我需要使用不同的访问路径?
    • 编辑:我想通了。应该是Path=Resources[Person_Template]
    • 您可以尝试使用 {StaticResource Person_Template} 代替绑定吗?
    • 那是我的问题。它无法找到StaticResource,因为它是从ItemsTemplate 内部的DataContext 引用的,而ItemsTemplate 本身又是存储在.Resources 中的另一个模板内部。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多