我使用ReactiveUIReactiveLists,在我的項目,這ReactiveLists之一綁定到ItemsSource
DependencyProperty
在我的自定義WPF MultiSelectComboBox:ReactiveUI ReactiveList和的DependencyProperty
<CustomControls:MultiSelectComboBox x:Name="CountriesCombobox"
Width="200"
VerticalContentAlignment="Center"
ItemsSource="{Binding Repository.Countries}"
SelectedItems="{Binding Repository.SearchViewSelectedCountries, Mode=TwoWay}"/>
...
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable<object>), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(IEnumerable<object>), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
public IEnumerable<object> SelectedItems
{
get { return (IEnumerable<object>)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
public IEnumerable<object> ItemsSource
{
get { return (IEnumerable<object>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
而在我的ComboBox中選擇一個選項後SetSelectedItems
方法被稱爲:
private void SetSelectedItems()
{
if (SelectedItems == null)
SelectedItems = new List<object>();
foreach (Node node in _nodeList)
{
if (node.IsSelected && node.Title != "All")
{
if (this.ItemsSource.Count() > 0)
{
var temp = SelectedItems.ToList();
temp.Add(this.ItemsSource.First(x => x.ToString() == node.Title));
SelectedItems = SelectedItems.Concat(temp);
}
}
}
}
在這裏,我嘗試添加SelectedItems
到SelectedItemsProperty
,我的問題是,如果我的ItemsSourceProperty
有不同的類型比IEnumerable
結合沒有工作和財產是空的(我曾經嘗試類型:List
,IList
,ICollection
, ReactiveList
,IReactiveList
),如果它是作爲IEnumerable
System.InvalidCastException
被拋出時,我試圖投SelectedItems
列出:
var temp1 = (List<object>)SelectedItems;
如果我將嘗試使用Concat方法樣以上,SelectedItems
設置爲null
。所以我的問題是我如何添加alements到SelectedItems
當它被綁定到ReactiveList
?
它的建立accordig到this文章。
嘗試使SelectedItems一個ObservableCollection。順便說一句,我認爲持有對象類型的集合是不錯的做法。 –
改變到的ObservableCollection我的列表後是空的,不知何故結合沒有工作 – Carlos28
對不起,也許我失去了一些東西,但如果是ReactiveList這裏?此外,在您複製的示例中,SelectedItems屬性被定義爲Dictionary(指出可以使用List),爲什麼將其更改爲IEnumerable? –