我正在使用ReactiveUI ReactiveLists,在我的项目中,其中一个ReactiveLists在我的自定义WPF MultiSelectComboBox中绑定到ItemsSource
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
转换为列表时抛出{1}}:
SelectedItems
如果我尝试使用上面示例的Concat方法,则var temp1 = (List<object>)SelectedItems;
设置为SelectedItems
。所以我的问题是,如果将{+ 1}}绑定到null
,我该如何添加商品?
它是根据this文章创建的。
答案 0 :(得分:0)
嗯,在我使用IList interface:
之后,它工作了public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public IList SelectedItems
{
get { return (IList)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
然后:
private void SetSelectedItems()
{
SelectedItems.Clear();
foreach (Node node in _nodeList)
{
if (node.IsSelected && node.Title != "All")
{
if (this.ItemsSource.Count > 0)
SelectedItems.Add(this.ItemsSource.Cast<object>().ToList().First(x => x.ToString() == node.Title));
}
}
}