简体   繁体   中英

PrinterQueueStatus to Visibility converter on DataGridRow

I try to hide DataGridRow s under the conditiion that a PrintQueue.Status != PrintQueueStatus.None but I can't make it work.

Here's what I've tried:

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>

And my code-behind for the converter

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

So I want to know what I'm doing wrong? I'm pretty sure I've misunderstood the converter thing. Can anyone explain to me on my example? That would be awesome.

I've tried to cut the code to the important parts. So don't wonder if it looks like something missing. The program runs fine, just the rows won't hide.

You can either use value converter or trigger to do this, but you mixed them together.

Use value converter:

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

or

Use trigger, for the PrintQueueStatus enumeration, first declare a namespace for System.Printing .

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

Then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM