当引用不在ItemsSource中时,WPF绑定到ComboBox SelectedItem

juh*_*arr 6 c# wpf binding combobox

PageMediaSize将a 的集合绑定PrintQueueItemSourcea ComboBox(这很好).然后我绑定SelectedItemComboBoxDefaultPrintTicket.PageMediaSizePrintQueue.虽然这会将所选值设置为DefaultPrintTicket.PageMediaSize恰好,但它不会将最初选择的值设置为ComboBox初始值.DefaultPrintTicket.PageMediaSize 这是因为DefaultPrintTicket.PageMediaSize引用与集合中的任何引用都不匹配.但是我不希望它通过引用来比较对象,而是通过值来比较,但PageMediaSize不会覆盖Equals(我无法控制它).我真正想做的是设置一个IComparableComboBox使用,但我没有看到任何方法这样做.我试过用一个Converter,但我需要的不仅仅是价值而且我无法弄清楚如何将集合传递给ConverterProperty.关于如何处理这个问题的任何想法.

这是我的xaml

<ComboBox x:Name="PaperSizeComboBox" 
          ItemsSource="{Binding ElementName=PrintersComboBox, Path=SelectedItem, 
                        Converter={StaticResource printQueueToPageSizesConverter}}"
          SelectedItem="{Binding ElementName=PrintersComboBox, 
                         Path=SelectedItem.DefaultPrintTicket.PageMediaSize}"
          DisplayMemberPath="PageMediaSizeName"
          Height="22"
          Margin="120,76,15,0"
          VerticalAlignment="Top"/>
Run Code Online (Sandbox Code Playgroud)

以及获取PageMediaSize集合的转换器的代码

public class PrintQueueToPageSizesConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture)
    {
        return value == null ? null :
            ((PrintQueue)value).GetPrintCapabilities().PageMediaSizeCapability;
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
                          System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

我之前尝试过DefaultPrintTicket.PageMediaSize在集合中设置相应的引用InitializeComponent,但是没有用.当我从中选择一些东西时,它肯定会设置值ComboBox,但它似乎没有其他方式.

geh*_*hho 3

是否可以为该包装类创建一个包装类PageMediaSize并覆盖Equals(object)该包装类中的方法?然后,您可以将此包装器类的实例添加到您的集合中,以便不再通过引用比较它们。当然,您将需要一些代码来包装和展开PageMediaSize实例,但这是我能想象的最好方法。