【问题标题】:How to access property of DataGrid ItemsSource object in DataGridRow.CellStyle?如何访问 DataGridRow.CellStyle 中 DataGrid ItemsSource 对象的属性?
【发布时间】:2017-12-26 02:12:09
【问题描述】:


首先,让我向您展示我的课程:

public class Model
{
    public Model Parent {get; set;}
    public ObservableCollection<Model> Childs {get; set;}
    public ObservableCollection<Partner> Partners {get; set;}
    public bool IsTopElement => Parent == null;
}

public class Partner
{
    public bool IsActive {get; set;}
    public string PartnerRole {get; set;}
    public bool IsCustomer => PartnerRole == "Customer"
}

如您所见,Model 是一个层次结构,因为它可以有一个父级和一个子级列表。我有一个 UserControl,其 DataContext 设置为 Model 的实例。在这个 UserControl 中,我有一个 DataGrid,它的 ItemsSource 绑定到 Partners。在这个 DataGrid 中,我有一个 DataGridCheckBoxColumn,它绑定到 Partner 对象的 IsActive 属性和几个 DataGridTextColumns。 我想要实现的是,如果当前 Model 对象不是顶部元素(没有父元素),则属于 Partner 条目的所有 IsActive-Checkbox 单元格,其 IsCustomer == True 应被禁用。
我尝试了以下 XAML 代码:

<UserControl DataContext="{Binding Model}">
    <DataGrid ItemsSource="{Binding Partners}" 
              AutoGenerateColumns="False"
              CanUserResizeRows="False"
              CanUserAddRows="False"
              CanUserDeleteRows="False"
              RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding IsActive}">
                <DataGridCheckBoxColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding Model.IsTopElement}" Value="False"/>
                                    <Condition Binding="{Binding ???}"/> <!-- I'd have expected Binding="{Binding IsCustomer}" Value="True" to work, but it doesn't-->
                                </MultiDataTrigger.Conditions>
                                <Setter Property="IsEnabled" Value="False"/>
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridCheckBoxColumn.CellStyle>
            </DataGridCheckBoxColumn>
        </DataGrid.Columns>             
    </DataGrid>
</UserControl>

我现在面临的问题是我无权访问MultiDataTrigger的第二个条件中DataGrid中Partner对象的属性IsCustomer。那么我怎样才能在那里访问这个属性呢?另外,我没想到在第一个条件中可以访问 ModelIsTopElement 属性,因为 DataGrid 的 ItemsSource 绑定到 Partners 但显然我有,这让我很困惑。
任何帮助将非常感激。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: c# wpf xaml binding datagrid


    【解决方案1】:

    DataGridCellDataContext 是一个 Partner 对象。您可以使用{RelativeSource} 绑定到父UserControlDataContext。试试这个:

    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding DataContext.IsTopElement, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="False"/>
                    <Condition Binding="{Binding IsCustomer}" Value="True"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="IsEnabled" Value="False"/>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 感谢您的回答!但是,我认为你误解了一些东西。给我带来麻烦的不是ModelIsTopElement 属性,而是我无法访问的DataGridRow 中Partner 对象的IsCustomer 属性。我试过你的方法。不幸的是,&lt;Condition Binding="{Binding DataContext.IsCustomer, RelativeSource={RelativeSource AncestorType=DataGridCell}}" Value="False"/&gt; 不适用于我的第二种情况。 VS 说DataContext.IsCustomer 无法解决。我可以运行应用程序,但没有达到预期的行为。
    • 你怎么知道哪个属性给你带来了麻烦?合作伙伴没有任何模型属性,那么您的第一个绑定为什么会起作用?您在这里没有给出完整的图片...由于 Partner 类有一个 IsCustomer 属性,您应该能够像我展示的那样直接绑定到这个。
    • 怎么样?应该可以工作,因为 Partners 和 IsTopElement 属于同一类。至少在您在此处发布的代码中。根据您提供的信息,您的问题无法重现。
    • 我对您的建议进行了更多尝试,并将 AncestorType 更改为 DataGridRow 成功了 :) 我知道它必须是第二个不起作用的绑定,因为没有它,Checkboscolumn 是根据需要禁用。我以对我有用的方式编辑了您的答案并接受了它。再次感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2017-05-29
    • 1970-01-01
    • 2019-06-02
    相关资源
    最近更新 更多