可见绑定基于绑定对象和模型属性绑定、属性、模型、对象

2023-09-06 07:58:08 作者:帝王不怀旧

我跑的独特情况今天在这里,我需要绑定可见在按钮的财产 DataGridRow 要基于模型的被装订物的两个属性和支持它。

I ran in to the unique situation today where I needed to bind the Visible property of a button in a DataGridRow to be based on both a property of the bound object and of the model backing it.

XAML:

<t:DataGrid ItemsSource="{Binding Items}">
    <t:DataGrid.Columns>
        <t:DataGridTemplateColumn>
            <t:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Visibility="IsEditable OR IsAdmin"/>
                </DataTemplate>
            </t:DataGridTemplateColumn.CellTemplate>
        </t:DataGridTemplateColumn>
    </t:DataGrid.Columns>
</t:DataGrid>

型号:

class TheModel
{
    public ObservableCollection<Whatever> Items { get; set; }
    public bool IsAdmin { get; set; }
}

类:

class Whatever
{
    public bool IsEditable { get; set; }
}

这难倒我。我能想到的可能工作唯一的概念会以某种方式传递绑定的对象,要么整个模型或仅 IsAdmin 属性设置为一个静态方法上的转换器或什么的。任何想法?

This stumped me. The only concept that I could think might work would be somehow passing the bound object and either the entire model or just the IsAdmin property to a static method on a converter or something. Any ideas?

推荐答案

首先,你不能直接使用布尔的能见度。你需要使用 BooleanToVisibilityConverter 。

Firstly, you cannot directly use a Boolean for Visibility. You need to use the BooleanToVisibilityConverter.

其次,有关的或的,你有不同的选择:

Secondly, about the OR, you have different options:

在创建