C# 从dxe:ComboBoxEdit获取选定值
我正在使用DevExpress ComboboxEdit对象从用户处获取多个选择。我的问题是,我不确定一旦选择完成,会返回什么类型的对象 我已经阅读了,并提出了下面的代码,但我不确定我遗漏了什么。(我也不知道什么是C# 从dxe:ComboBoxEdit获取选定值,c#,xaml,devexpress,C#,Xaml,Devexpress,我正在使用DevExpress ComboboxEdit对象从用户处获取多个选择。我的问题是,我不确定一旦选择完成,会返回什么类型的对象 我已经阅读了,并提出了下面的代码,但我不确定我遗漏了什么。(我也不知道什么是dependencProperty,但希望避免太多对象) MainWindow.xaml.cs using System.Collections.Generic; using System.Windows; using System.Text; namespace Demo {
dependencProperty
,但希望避免太多对象)
MainWindow.xaml.cs
using System.Collections.Generic;
using System.Windows;
using System.Text;
namespace Demo
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private System.Collections.Generic.IList<string> _myList;
private System.Collections.Generic.IList<string> _mySelectedList; // This has probably the wrong type.
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public IList<string> MyList
{
get
{
return _myList;
}
set
{
_myList = value;
this.RaisePropertyChanged("MyList");
}
}
public IList<string> MySelectedList
{
get
{
return _mySelectedList;
}
set
{
_mySelectedList = value;
this.RaisePropertyChanged("MySelectedList");
}
}
private void showSelected(object sender, RoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(string s in this.MySelectedList)
{
sb.Append(s);
}
System.Windows.MessageBox.Show(sb.ToString());
// This MessageBox show show whatever is checked.
}
public MainWindow()
{
MySelectedList = new System.Collections.Generic.List<string>();
MyList = new System.Collections.Generic.List<string>();
MyList.Add("a");
MyList.Add("b");
MyList.Add("c");
MyList.Add("d");
DataContext = this;
}
}
}
使用System.Collections.Generic;
使用System.Windows;
使用系统文本;
名称空间演示
{
公共部分类主窗口:窗口,INotifyPropertyChanged
{
private System.Collections.Generic.IList\u myList;
private System.Collections.Generic.IList _mySelectedList;//这可能是错误的类型。
公共事件属性更改事件处理程序属性更改;
受保护的void RaisePropertyChanged(字符串propertyName)
{
var handler=this.PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共图书馆
{
得到
{
返回我的列表;
}
设置
{
_myList=值;
此.RaisePropertyChanged(“MyList”);
}
}
公共IList MySelectedList
{
得到
{
返回_mySelectedList;
}
设置
{
_mySelectedList=值;
此.RaisePropertyChanged(“MySelectedList”);
}
}
已选择私有void show(对象发送方、路由目标方)
{
StringBuilder sb=新的StringBuilder();
foreach(此.MySelectedList中的字符串s)
{
某人追加;
}
System.Windows.MessageBox.Show(sb.ToString());
//此消息框显示选中的内容。
}
公共主窗口()
{
MySelectedList=new System.Collections.Generic.List();
MyList=new System.Collections.Generic.List();
MyList.添加(“a”);
MyList.添加(“b”);
MyList.添加(“c”);
MyList.添加(“d”);
DataContext=this;
}
}
}
当我运行它并单击组合框时,会出现一个红色的X
,表示类型System.Collection.Generic.List`1[System.Object]无法转换。消息框总是空的。您没有在主窗口上实现,但这可能不是唯一的问题。在你真正尝试修补WPF之前,我会仔细阅读。如果你不理解这些概念,一切都会变得困难和混乱
编辑
他们使用的是一个依赖属性
(如您所述)。但不管怎样,这就是你要实现的方法
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(MainWindow), new PropertyMetadata(null, new PropertyChangedCallback(OnSelectedItemsChanged)));
private static void OnSelectedItemsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
MainWindow mainWindow = o as MainWindow;
if (mainWindow != null)
mainWindow.OnSelectedItemsChanged((IList)e.OldValue, (IList)e.NewValue);
}
protected virtual void OnSelectedItemsChanged(IList oldValue, IList newValue)
{
// Add your property changed side-effects. Descendants can override as well.
}
public IList SelectedItems
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (IList)GetValue(SelectedItemsProperty);
}
set
{
SetValue(SelectedItemsProperty, value);
}
}
请注意,它的类型必须是IList
,您需要强制转换为string
另外,删除Mode=TwoWay
,因为绑定中不需要它
<dxe:ComboBoxEdit ItemsSource="{Binding MyList}" EditValue="{Binding SelectedItems}" >
<dxe:ComboBoxEdit.StyleSettings>
<dxe:CheckedComboBoxStyleSettings/>
</dxe:ComboBoxEdit.StyleSettings>
</dxe:ComboBoxEdit>
您也不需要INotifyPropertyChanged
这是我的错误。我以为你在做传统绑定。EditValue
属性包含一个对象列表,所以你在VM中的代码应该如下所示:
private List<object> _mySelectedList;
public List<object> MySelectedList
{
get
{
return _mySelectedList;
}
set
{
_mySelectedList = value;
this.RaisePropertyChanged("MySelectedList");
}
}
private List\u mySelectedList;
公共列表MySelectedList
{
得到
{
返回_mySelectedList;
}
设置
{
_mySelectedList=值;
此.RaisePropertyChanged(“MySelectedList”);
}
}
或者您可以编写EditValue
converter,示例如下。尝试在上实现System.ComponentModel.INotifyPropertyChangedMainWindow@Carson完成后,也在setters中引发了事件,没有可见的效果。我应该以某种方式注册处理程序还是WPF为我注册视图元素?您不需要注册任何处理程序。请更新您的代码,我将尝试更新我的答案。@Carson刚刚更新了代码为什么DependencyProperty是奇数?你有更好的主意吗?不,我想这本身并不奇怪,但我觉得找到一种更传统的管理绑定的方法会更直观。例如,如果他们有一个“SelectedItems”属性,您可以绑定到返回IEnumerable
或其他内容的属性。通常不需要创建一个新的dependencProperty
来绑定到属性IMO,如果您想维护严格的MVVM,这肯定没有帮助。但是,您也可以始终绑定到您的DependencyProperty
,因此我认为有一种方法可以绕过它。我可能是过于固执己见了。我的抱怨更多的是大声思考DevExpress有时处理绑定的方式,但这只是我的观点,没有什么意义。
private List<object> _mySelectedList;
public List<object> MySelectedList
{
get
{
return _mySelectedList;
}
set
{
_mySelectedList = value;
this.RaisePropertyChanged("MySelectedList");
}
}