Я использую объект DevExpress ComboboxEdit для получения множественного выбора от пользователя. Моя проблема заключается в том, что я не уверен, какой тип объекта будет возвращен после выбора.Получить выбранные значения из dxe: ComboBoxEdit
Я прочитал this one и придумал код ниже, но я не уверен, что мне не хватает. (Я не знаю точно, что DependencyProperty
есть, но хотелось бы, чтобы избежать слишком много объектов)
<Window x:Class = "Demo.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local = "clr-namespace:Demo"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxl="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
mc:Ignorable = "d"
Title = "MainWindow" Height = "350" Width = "525">
<StackPanel>
<dxe:ComboBoxEdit ItemsSource="{Binding Path=MyList}"
IsTextEditable="False"
EditValue="{Binding Path=MySelectedList, Mode=TwoWay}"
Name="abc">
<dxe:ComboBoxEdit.StyleSettings>
<dxe:CheckedComboBoxStyleSettings/>
</dxe:ComboBoxEdit.StyleSettings>
</dxe:ComboBoxEdit>
<Button Click="showSelected" Content="Show selected items" />
</StackPanel>
</Window>
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;
}
}
}
Когда я запускаю его и нажмите на выпадающий, затем появляется красный X
и говорит, что The type System.Collection.Generic.List´1[System.Object] could not be converted
. И MessageBox всегда пуст.
Попробуйте внедрить System.ComponentModel.INotifyPropertyChanged на MainWindow – Carson
@Carson Done, также поднял событие в сеттерах, без видимого эффекта. Должен ли я как-то регистрировать обработчики или WPF регистрирует элементы представления для меня? –
Вам не нужно регистрировать обработчиков. Обновите свой код, и я попытаюсь обновить свой ответ. – Carson