[英]MVVM - get selected items from ListBox via Data Binding
I am writing app using MVVM pattern in C#.我正在使用 C# 中的 MVVM 模式编写应用程序。
My goal is to get selected items from ListBox in my own User Control.我的目标是从我自己的用户控件中的 ListBox 中获取所选项目。
I have created bindable object, with method to change this object (called when something new is selected):我创建了可绑定对象,并带有更改此对象的方法(在选择新对象时调用):
public partial class MyUserControl : UserControl
{
...
public IEnumerable SelectedItems
{
get { return (IEnumerable)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(IEnumerable),
typeof(MyUserControl),
new FrameworkPropertyMetadata(default(IEnumerable),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public MyUserControl ()
{
InitializeComponent();
}
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedItems = ListBox.SelectedItems;
}
}
There is also Items part, and in xaml part ListBox is named ListBox:还有 Items 部分,在 xaml 部分 ListBox 被命名为 ListBox:
<ListBox Name="ListBox" SelectionChanged="SelectionChanged" ... />
This how it looks in page with ViewModel, in which MyUserControl is created:这是它在 ViewModel 页面中的外观,其中创建了 MyUserControl:
<uc:MyUserControl ... SelectedItems="{Binding Path=MyObjectItems}" />
And here comes the problem.问题来了。 When setting SelectedItems in ViewModel:
在 ViewModel 中设置 SelectedItems 时:
private ObservableCollection<MyObject> _myObjectItems;
public ObservableCollection<MyObject> MyObjectItems
{
get { return _myObjectItems; }
set { _myObjectItems = value; }
}
No matter what I do, value
will always be null.无论我做什么,
value
总是为空。 This also means, that SelectedItems
in MyUserControl
is null, too.这也意味着,
MyUserControl
中的SelectedItems
也是 null。
I can, for example use OneWayToSource
binding mode:例如,我可以使用
OneWayToSource
绑定模式:
<uc:MyUserControl ... SelectedItems="{Binding Path=MyObjectItems, Mode=OneWayToSource}" />
value
is still null, same as MyObjectItems
, but at least SelectedItems
in MyUserControl
contains selected items. value
仍然为 null,与MyObjectItems
相同,但至少MyUserControl
中的SelectedItems
包含所选项目。 Not good enough :/还不够好 :/
After hours of trying different approaches I've found NuGet package Extended WPF Toolkit .经过数小时的尝试不同的方法,我发现了 NuGet 包Extended WPF Toolkit 。 List
SelectedItemsOverride
from class CheckListBox
allows to bind list of selected items:来自类
CheckListBox
List SelectedItemsOverride
允许绑定所选项目的列表:
<UserControl x:Class="View.UserControls.MyUserControl"
...
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
x:Name="Root">
<Grid>
<xctk:CheckListBox Name="ListBox"
ItemSelectionChanged="SelectionChanged"
SelectedItemsOverride="{Binding SelectedItems, ElementName=Root}"
... />
</Grid>
</UserControl>
And because of that, binding:正因为如此,绑定:
<uc:MyUserControl ... SelectedItems="{Binding Path=MyObjectItems}" />
works!作品! I have access to selected items in View Model - everything in simple way.
我可以访问视图模型中的选定项目 - 一切都以简单的方式进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.