XAML shows which default printer is installed

<UserControl x:Class="MyApp.PrinterSelection"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:Printing="clr-namespace:System.Drawing.Printing;assembly=System.Drawing"
             xmlns:local="clr-namespace:MyApp"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

<Grid>

        <ListBox x:Name="displayInstalledPrinterListView" HorizontalAlignment="Left" Height="311" Margin="10,0,0,0" VerticalAlignment="Top" Width="499" ItemsSource="{x:Static Printing:PrinterSettings.InstalledPrinters}" SelectionChanged="displayInstalledPrinterListView_SelectionChanged" AlternationCount="2" FontSize="16"/>
    </Grid>

      

how can I tell which printer in the ListBox is the default printer using XAML. If this can't be done with XAML, what's the best approach?

I know that I can programmatically check each printer to make sure the IsDefaultPrinter is true. However, I wanted to know if this can be done with XAML (only)

+3


source to share


2 answers


It is not clear to me exactly what you are facing and how you want to change the visual display of printer names. So here's a generic example of a XAML only implementation that displays all installed printers along with the name of the current default printer:

<Window x:Class="TestSO30225596DefaultPrinter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Printing="clr-namespace:System.Printing;assembly=System.Printing"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <Printing:LocalPrintServer x:Key="localPrintServer1"/>
    <ObjectDataProvider x:Key="printerCollection"
                        ObjectInstance="{StaticResource localPrintServer1}"
                        MethodName="GetPrintQueues">
      <ObjectDataProvider.MethodParameters>
        <x:ArrayExtension Type="{x:Type Printing:EnumeratedPrintQueueTypes}">
          <Printing:EnumeratedPrintQueueTypes>Local</Printing:EnumeratedPrintQueueTypes>
          <Printing:EnumeratedPrintQueueTypes>Connections</Printing:EnumeratedPrintQueueTypes>
        </x:ArrayExtension>
      </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
  </Window.Resources>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ListBox x:Name="displayInstalledPrinterListView"
             HorizontalAlignment="Left" VerticalAlignment="Top"
             Height="311" Width="499" Margin="10,0,0,0"
             ItemsSource="{Binding Source={StaticResource printerCollection}}"
             AlternationCount="2" FontSize="16">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Name}"/>
          </StackPanel>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    <StackPanel Orientation="Horizontal" Grid.Row="1">
      <TextBlock Text="Default Printer: "/>
      <TextBlock Text="{Binding Source={StaticResource localPrintServer1}, Path=DefaultPrintQueue.Name}"/>
    </StackPanel>
  </Grid>
</Window>

      



Please note that you will need to add a reference to the WPF-compatible assembly System.Printing.dll

. The above is technically in line with your broad specification, that is, it indicates (via the text displayed below ListBox

) the default printer.

I believe that given the above example, you can modify it to suit your specific needs, using the property value DefaultPrintQueue.Name

to compare against the actual name of each printer, and provide whatever readings you think are most appropriate based on that.

+1


source


You can use the following code:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding Printers}">
            <ComboBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:PrinterWrapper}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="Default - " x:Name="default" Visibility="Collapsed"/>
                        <TextBlock Text="{Binding PrinterName}" Grid.Column="1" />
                    </Grid>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding IsDefaultPrinter}" Value="True">
                            <Setter TargetName="default" Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

      



Code for:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Printing;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }

    public class ViewModel
    {
        public ObservableCollection<PrinterWrapper> Printers { get; set; }

        public ViewModel()
        {
            this.Printers = new ObservableCollection<PrinterWrapper>();

            LocalPrintServer server = new LocalPrintServer();
            var printQueues = server.GetPrintQueues(new EnumeratedPrintQueueTypes[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
            foreach (var printQueue in printQueues)
            {
                var printerWrapper = new PrinterWrapper();
                printerWrapper.PrinterName = printQueue.Name;
                printerWrapper.IsDefaultPrinter = (printQueue.Name == server.DefaultPrintQueue.Name);

                this.Printers.Add(printerWrapper);
            }
            server.Dispose(); // Depends on you :-)
        }
    }
    public class PrinterWrapper
    {
        public string PrinterName { get; set; }
        public bool IsDefaultPrinter { get; set; }
    }
}

      

Note. PrinterWrapper

a class that you can use to host additional printer related properties and in ViewModel

you can write your own printing logic. Also, instead of only showing the default text in front of the default printer name, you can use some kind of image or some other fancy stuff.

+1


source







All Articles