【问题标题】:How to bind a style to a property of whatever datacontext has the styled element?如何将样式绑定到具有样式元素的任何数据上下文的属性?
【发布时间】:2012-03-10 16:41:19
【问题描述】:

我在如下样式触发器中有一个数据触发器

<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=APropertyOfTheDataGridCellContext Bind DataContext}" Value="True" >
            ...
        </DataTrigger>
    </Style.Triggers>
</Style>

我将使用属性 CellStyle 将此样式应用于某些 DataGridColumns。我想要的是将样式的 DataTrigger 绑定到单元格的数据上下文的属性,这样我就可以在所有必需的列上使用此样式,而不必为每列创建一个新样式。这可能吗?

提前致谢。

编辑:澄清一下,DataGrid 的 DataContext 是一个 ViewModel,它有一个名为 Rows 的属性,而 Rows 有一个名为 Cells 的属性和一个名为 Blink 的属性。我想像这样引用样式中的属性:

<DataGrid.Resources>
<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Blink}" Value="True" >
            ...
        </DataTrigger>
    </Style.Triggers>
</Style>
</DataGrid.Resources>

这给了我一个错误,因为 DataGridResources 数据上下文是包含行而不是单元格的那个。如何从样式中引用 DataGridCell DataContext?

【问题讨论】:

    标签: wpf binding datagrid datacontext


    【解决方案1】:

    删除x:Key 属性,然后该属性将应用于所有列。

    <Style TargetType="{x:Type DataGridCell}"> 
        <Style.Triggers>         
            <DataTrigger Binding="{Binding Path=DataContext.MyProperty,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridCell}}}" Value="True" />                       
       </Style.Triggers>
    </Style>
    

    如果您有 2 个 DataGrid,则应将此样式保留在要修改的 dataGrid 的 Resources 标记中...

    如果 RelativeSource 不起作用,我希望通过触发器更改列的模板以实现类似的行为

    为列编写触发器

    <WpfToolkit:DataGridTemplateColumn MinWidth="50"
                                       MaxWidth="80"
                                       Header="MyHeader"
                                       IsReadOnly="True">
        <WpfToolkit:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <DataTemplate.Resources>
                    <DataTemplate x:Key="normalTemplate" />
                    <DataTemplate x:Key="ChangeTempalte">
                       <!-Your Controls->
                    </DataTemplate>
                </DataTemplate.Resources>
                <ContentPresenter x:Name="chnagedField"
                                  Content="{Binding}"
                                  ContentTemplate="{StaticResource ResourceKey=normalTemplate}" />
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=MyProperty}" Value="True">
                        <Setter TargetName="EditableField" Property="ContentTemplate" Value="{StaticResource ResourceKey=chnageTempalte}" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </WpfToolkit:DataGridTemplateColumn.CellTemplate>
    </WpfToolkit:DataGridTemplateColumn>  
    

    这里MyPropertyDataGridRowDataContext 的属性,因此您可以选择任何级别的嵌套属性哟比较该特定行...唯一的缺点是您必须为所有人执行此操作需要这个的列

    希望对你有帮助

    【讨论】:

    • 我面临的问题是我在 DataGrid 资源中声明样式。我可以从那里引用 DataGridCell DataContext 的属性吗?
    • @SoMoS 你想做这样的事情吗...查看已编辑的答案
    • 对,类似这样,但数据上下文不是祖先,而是孩子。你知道我的意思吗?
    • @SoMoS 孩子 ?? ... DataGridCell 的DataContext 将包含来自DataGridRowDataContext 的特定属性...所以我假设DataGridCell DataContext 具有所需的属性或其子属性。 .. 您可以将其写为Binding="{Binding Path=DataContext.MyProperty.PropertyChild,Relativeresource={RelativeSource Mode=FindAscestor,AncestorType={x:Type DataGridCell}}" Value="True" DataGridCell 的 DataContext 将是您在 Xaml 中设置的对象,用于绑定 DataGrid 的列...
    • 我的意思是你正在使用 FindAncestor 进行绑定。由于 Style 在 DataGrid.Resources 中,绑定将尝试解析绑定以寻找 DataGrid 的祖先,对吗?
    猜你喜欢
    • 1970-01-01
    • 2014-03-01
    • 2015-02-09
    • 2013-06-20
    • 2014-03-28
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多