juh*_*arr 6 c# wpf binding combobox
我PageMediaSize
将a 的集合绑定PrintQueue
到ItemSource
a ComboBox
(这很好).然后我绑定SelectedItem
的ComboBox
到DefaultPrintTicket.PageMediaSize
的PrintQueue
.虽然这会将所选值设置为DefaultPrintTicket.PageMediaSize
恰好,但它不会将最初选择的值设置为ComboBox
初始值.DefaultPrintTicket.PageMediaSize
这是因为DefaultPrintTicket.PageMediaSize
引用与集合中的任何引用都不匹配.但是我不希望它通过引用来比较对象,而是通过值来比较,但PageMediaSize
不会覆盖Equals(我无法控制它).我真正想做的是设置一个IComparable
供ComboBox
使用,但我没有看到任何方法这样做.我试过用一个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
,但它似乎没有其他方式.
是否可以为该包装类创建一个包装类PageMediaSize
并覆盖Equals(object)
该包装类中的方法?然后,您可以将此包装器类的实例添加到您的集合中,以便不再通过引用比较它们。当然,您将需要一些代码来包装和展开PageMediaSize
实例,但这是我能想象的最好方法。