DataGridRow 上的 PrinterQueueStatus 到可见性转换器

PrinterQueueStatus to Visibility converter on DataGridRow

我尝试在 PrintQueue.Status != PrintQueueStatus.None 的条件下隐藏 DataGridRow,但我无法让它工作。

这是我尝试过的方法:

XAML

<Window.Resources>
<self:PrinterStatusToVisibilityConverter x:Key="PrinterStatusToVisibilityConverter" />
    </Window.Resources>

<DataGrid RowDetailsVisibilityMode="Visible" AlternatingRowBackground="#E0E0E0" AlternationCount="2"  CellStyle="{StaticResource BodyContentDataGridCentering }" Grid.Row="1" Name="dgPrinters" AutoGenerateColumns="False" RowHeight="50">
    <!--body content datagrid cell vertical centering-->
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Überwachen" Width="Auto" CellStyle="{StaticResource BodyContentDataGridCentering}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox HorizontalAlignment="Center" IsChecked="{Binding Monitor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Width="Auto" Header="Druckername" Binding="{Binding FullName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
        <DataGridTextColumn Width="Auto" Header="Freigabename" Binding="{Binding ShareName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Portname" Binding="{Binding PortName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Treibername" Binding="{Binding DriverName, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True"/>
    </DataGrid.Columns>

    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status, Converter={StaticResource PrinterStatusToVisibilityConverter}}">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

还有我的代码隐藏转换器

public class PrinterStatusToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((PrintQueueStatus) value != PrintQueueStatus.None)
            return Visibility.Visible;

        return Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return PrintQueueStatus.None;
    }
}

所以我想知道我做错了什么?我很确定我误解了转换器的事情。谁能用我的例子向我解释?那太棒了。

我试图将代码缩减到重要部分。所以不要怀疑它是否看起来像缺少什么。程序运行正常,只是行不会隐藏。

您可以使用值转换器或触发器来执行此操作,但您将它们混合在一起了。

使用值转换器:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">                
        <Setter Property="Visibility" Value="{Binding Status, Converter={StaticResource PrinterStatusToVisibilityConverter}}" />
    </Style>
</DataGrid.RowStyle>

使用触发器,对于PrintQueueStatus枚举,先为System.Printing声明一个命名空间。

<Window x:Class="..."
    xmlns:self="..."
    xmlns:printing="clr-namespace:System.Printing;assembly=System.Printing">

然后

    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}">
                    <DataTrigger.Value>
                        <printing:PrintQueueStatus>None</printing:PrintQueueStatus>
                    </DataTrigger.Value>
                    <Setter Property="Visibility" Value="Hidden" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>