XAML显示哪个安装的打印机是默认的

时间:2015-05-13 21:36:25

标签: c# wpf xaml printing

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

如何指示ListBox中的哪台打印机是使用XAML的默认打印机。如果用XAML无法做到这一点,那么最好的方法是什么?

我知道我可以以编程方式检查每台打印机,看看IsDefaultPrinter是否为真。但是我想知道是否可以使用XAML(仅限)

来完成

2 个答案:

答案 0 :(得分:1)

我并不完全清楚具体您遇到了什么问题,也不清楚您希望如何更改打印机名称的可视化显示。因此,这是一个仅用于XAML的实现的通用示例,它显示所有已安装的打印机以及当前默认打印机的名称:

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

请注意,您需要添加对WPF兼容的System.Printing.dll程序集的引用。以上技术上符合您的广泛规范,即它确实指出(通过ListBox下面显示的文本)哪个打印机是默认的。

我相信,鉴于上面的示例,您可以通过使用DefaultPrintQueue.Name属性值与每台打印机的实际名称进行比较来修改它以满足您的特定需求,并提供您认为最合适的任何指示在那。

答案 1 :(得分:1)

您可以使用以下代码:

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

代码背后:

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

注意:PrinterWrapper类可用于容纳更多与打印机相关的属性,在ViewModel中,您可以编写打印逻辑。此外,您可以使用一些图像或其他一些花哨的东西,而不是仅显示默认打印机名称前的文本Default。