DataGridRow上的PrinterQueueStatus到Visibility转换器

时间:2016-01-28 14:53:29

标签: c# wpf xaml datatrigger ivalueconverter

我试图将DataGridRow隐藏在PrintQueue.Status != PrintQueueStatus.None的条件下,但我无法使其发挥作用。

这是我尝试过的:

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;
    }
}

所以我想知道我做错了什么?我很确定我误解了转换器的事情。有人可以向我解释一下我的例子吗?那太棒了。

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

1 个答案:

答案 0 :(得分:1)

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

使用值转换器:

<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>