解答WPF中ComboBox SelectedItem Binding不上的Bug

正在做一个打印机列表,从中选择一个打印机(System.Printing)

<ComboBox Width="150"
                      ItemsSource="{Binding PrintQueues}"
                      SelectedItem="{Binding Model.CurrentPrintQueue}"
                      DisplayMemberPath="Name">
            </ComboBox>
var printServer = new LocalPrintServer();

            PrintQueues = printServer.GetPrintQueues();

            Model.CurrentPrintQueue = printServer.DefaultPrintQueue;

都觉得这样是没有问题的对不对,可是,实际运行起来 SelectedItem就是绑定不上,找了一大堆网站,都说ComboBox的一个Bug,换SelectedIndex,SelectedValue等方法,我想这不科学,这么优秀的控件,这么简洁的Binding,不可能有Bug的呀。

解决方法:

首先想SelectedItem肯定是来自于ItemsSource中的一个引用,并且他们是同一个对象才行。

但是看代码,我以为printServer.DefaultPrintQueue就是GetPrintQueues()中的一个对象,好嘛,这样写

Model.CurrentPrintQueue = PrintQueues.FirstOrDefault(x => x.Name == printServer.DefaultPrintQueue.Name);

居然绑定上了!

所以以后一定要记住了,SelectedItem必须是ItemsSource中的同一个对象才行,被PrintServer坑了

时间: 2024-08-06 22:50:32

解答WPF中ComboBox SelectedItem Binding不上的Bug的相关文章

WPF中ComboBox用法

The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, because the list of items is hidden when not needed. The ComboBox control is used many places in Windows, but to make sure that everyone knows how it looks

WPF中利用WebClient向服务器上传文件

转载:原文地址http://blog.csdn.net/wj1589300/article/details/9255631 WPF中利用WebClient向服务器上传文件 忽然接到一个任务,在WPF中上传文件至服务器~在网上搜了很多种方法,最终决定利用WebCient实现文件的上传工作,看似很简单的任务,却遇到了很多问题.先说一下我的探索步骤吧~ 一.选用WebClient.UploadFile方法 (String,String, String) [csharp] view plaincopyp

WPF中ComboBox绑定数据库自动读取产生数据

前台端 <ComboBox HorizontalAlignment="Left" Margin="410,113,0,0" VerticalAlignment="Top" Width="300"Name="cmb_SSBM" DisplayMemberPath="NAME" SelectedValuePath="CODE" SelectedIndex="

在 WPF 中如何在控件上屏蔽系统默认的触摸长按事件

来源:https://stackoverflow.com/questions/5962108/disable-a-right-click-press-and-hold-in-wpf-application 在控件上可以设置一个属性: Stylus.IsPressAndHoldEnabled 设置为 False 即可屏蔽触摸长按事件的触发. 参考文档:http://msdn.microsoft.com/en-us/library/system.windows.input.stylus.ispres

(WPF) MVVM: ComboBox Binding

基本思路还是在View的Xmal里面绑定ViewModel的属性,虽然在View的后台代码中也可以实现binding,但是还是在Xmal里面相对的代码量要少一些. 此例子要实现的效果就是将一个List<Customer> 绑定到一个ComboBox,并将选择后的Customer的Age显示在一个TextBlock中. 1. Model public class Customer { public string Name { get; set; } public int Age { get; s

WPF binding&lt;一&gt; Data Binding在WPF中的地位

在代码中经常看到      <Image Source="{Binding ElementName=LBoxImages, Path=SelectedItem.Source}" /> 或者是 <WrapPanel Orientation="Horizontal" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Li

【转】WPF中的Binding技巧(二)

WPF中的Binding技巧(二)   接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素的Name来进行绑定: 例子: <Window x:Name="MainWindow"> <Grid>               <Button Background="{Binding ElementName=MainWindow, Path=Bac

在WPF中让ListBox和ComboBox的快速检索功能失效

问题来源: 自定义一个ComboBox,用来显示日期.后台数据使用的是DateTime,经过Converter转化成“2015年01月01日”样子的成字符串用于显示. 但是,在实际使用中,不停的按下“[”键,光标会从以一个元素一下一下的向下移动. 经过调查,这是ComboBox的“快速检索”功能在作祟. 关于快速检索: WPF中ListBox和ComboBox有一个“快速检索”的功能. 比如在ListBox里,按下“a”键,光标会定位到第一个首字母为“a”的Item上. ComboBox也是一样

整理:WPF中Binding的几种写法

目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Binding DataTime}"/> <!--绑定到DataContext,并设置绑定模式--> <Button x:Name="btn" Content="{Binding DataTime,Mode=OneTime}"/> <!--绑定到DataContext,并设置更新模