WPF控件ComboBox 每个Item的ToolTip引发的异常
2013-08-02 14:49
344 查看
介绍
首先介绍下要实现的任务。做一个下拉框,当选择每个项的时候将鼠标发在上面显示该项的ToolTip的内容(Image)。实现
Model:public class SkinInfo : NotificationObject { private string name; private string assemblyName; public string AssemblyName { get { return assemblyName; } set { if (this.assemblyName != value) { assemblyName = value; base.RaisePropertyChanged("AssemblyName"); } } } public string Name { get { return name; } set { if (this.name != value) { name = value; base.RaisePropertyChanged("Name"); } } } public string Path { get; set; } [XmlIgnore] public ImageSource Image { get; set; } public SkinInfo() { } public SkinInfo(string skin) { var items = skin.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries); this.Name = items[0]; this.AssemblyName = items[1]; string uriStr = string.Format("/BasicInfoModule;component/Resources/{0}.png", this.Name); this.Image = new BitmapImage(new Uri(uriStr, UriKind.Relative)); this.Path=uriStr; } }
ViewModel:
public class DUTInfo : NotificationObject { public ObservableCollection<AccessoryInfo> Accessories { get; set; } [XmlIgnore] public ObservableCollection<SkinInfo> Skins { get; private set; } public DUTInfo() { this.Skins = new ObservableCollection<SkinInfo>( Config.Instance.Skins.Select<string, SkinInfo>(s => new SkinInfo(s))); } }
Xaml文件:
<ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding Skins}" SelectedValue="{Binding SelectedSkin}"> <ComboBox.ItemTemplate> <DataTemplate> <Label Content="{Binding Name}" Margin="0"> <Label.ToolTip> <Image Source="{Binding Image}" /> </Label.ToolTip> </Label> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
测试
此时实现啦功能,现在应该测试下,我打开界面点开下拉框,鼠标在每个Item的位置来回切换时,较短时间后会引发异常:Specified element is already the logical child of another element. Disconnect it first.
(意思:指定的元素已经是另一个元素的逻辑子元素。请先将其断开连接。)
然后我将图片的绑定属性改为 <Image Source="{Binding Path}" />,此时还会触发异常,但是鼠标在每个Item的位置来回切换时间较长,最总还会引发相同异常。
我想应该是:如果不清除他与前一个父控件的关系,则会报此类错误,解决办法是,再重新作为新的父控件的Content之前,清除他的父类绑定即可。
我仔细想了想ToolTip的功能不会这么弱吧,然后我在<Label.ToolTip>之间顺意输入看看包含了那些属性,我发现有一个<ToolTip>。
我加上之后,重新测试,很长一段时间,都没报错,姑且这么认为是解决了吧!!
最总Xaml文件代码ToolTip改为:
<Label.ToolTip> <ToolTip> <Image Source="{Binding Image}" /> </ToolTip> </Label.ToolTip>
相关文章推荐
- 闲话WinFrom与WPF(2) 控件篇——ComboBox
- WPF中TreeView控件如何获取选中的TreeViewItem对象?
- WPF之Binding基础五 使用集合对象作为列表控件的ItemSource
- BaseAdapter实现ListView中item上每个控件的获取以及点击
- WPF-004 控件 ToolTip
- 让 wpf tabcontrol 延缓初始化每个tab item content
- WPF 添加ACTIVEX 控件 ——System.Windows.Forms.AxHost+InvalidActiveXStateException异常
- android ListView中给Item的每个控件设置单击事件
- 【WPF】获取下拉列表(ComboBox)的选项(ComboBoxItem)的内容
- devpress GridControl控件绑定RepositoryItemImageComboBox 作为下拉框使用 zt
- 【Visual Studio风格开发系列 - WinForm设计时异常】关于VS.NET中窗体设计器初始化控件引发的设计时异常
- WPF的Combobox的ItemSource第二次绑定(更新)问题
- QTreeWidget中添加widget 让后清除item 引发的异常
- WPF 异常其他信息: “对类型“BaseControl.KImgButton”的构造函数执行符合指定的绑定约束的调用时引发了异常。”,行号为“38”,行位置为“22”。
- 【WPF】ComboBoxItem的禁用
- WPF控件之ComboBox
- ComBoBox控件,如果里面有item为空,那么就报ntdll.dll的解决办法
- pulltorefreshlistview 和xlistview 中的获取item控件布局时出现的类转换异常的问题:
- WPF 控件使用之ComboBox
- ListView遍历每个Item出现NullPointerException的异常