隐藏基于Property的DataGridTemplateColumn

时间:2014-09-22 08:52:12

标签: c# wpf binding datagrid

我有DataGridDataGridTemplateColumn个。

DataGrid的定义如下:

<DataGrid Grid.Row="1" Grid.Column="2" AutoGenerateColumns="False" 
    ItemsSource="{Binding SelectedFolder.Documents, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    SelectedItem="{Binding SelectedDocument, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Filename" Width="*" SortMemberPath="Titel">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Label Content="{Binding Titel}" VerticalAlignment="Center"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Format" Width="Auto" SortMemberPath="Extension">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Label Content="{Binding Extension}" VerticalAlignment="Center"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Created" Width="Auto" SortMemberPath="DateCreated">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Label Content="{Binding DateCreated, Converter={StaticResource DateConverter}}" VerticalAlignment="Center"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="User" Width="Auto" SortMemberPath="CreationUser">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Label Content="{Binding CreationUser, Converter={StaticResource UserIdToUserNameConverter}}" VerticalAlignment="Center"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Delete" Width="Auto" Visibility="{Binding DataContext.User.Permission, Converter={StaticResource UserPermissionToDeleteVisibility}, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Style="{StaticResource buttonStyle}" Command="{Binding DataContext.DialogActionCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
                            CommandParameter="{x:Static helper:ButtonActionTarget.DeleteDocument}" Width="24" Height="24" VerticalAlignment="Center">
                        <Image Source="{x:Static helper:ImageHelper.Delete}"/>
                    </Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

数据已正确更新。遗憾的是,最后一列的Visibility未正确应用。我在这做错了什么?我的转换器不会随时调用。

转换器看起来像:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value is UserPermission)
    {
        if ((((UserPermission)value) & UserPermission.Delete) == UserPermission.Delete)
            return Visibility.Visible;
    }
    return Visibility.Collapsed;
}

如果我在Visibility的{​​{1}}设置了Button - 属性,那就可以了。但我希望隐藏完整的专栏。

0 个答案:

没有答案