将 eventtocommand 操作动态添加到列表框
我有一个带有
命名空间的页面:
xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
设计时的 xaml:
<ListBox x:Name="ItemGroupsList" ItemsSource="{Binding ItemGroups}" Height="496"
SelectedItem="{Binding SelectedItemGroup, Mode=TwoWay}" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="SelectionChanged">
<GalaSoft_MvvmLight_Command:EventToCommand
x:Name="SelectionChangedEvent"
Command="{Binding GoToEditItemGroupCommand, Mode=OneWay}"
PassEventArgsToCommand="True"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
在代码中,我在运行时生成多个列表框,并且希望能够绑定到视图模式上的中继命令,如上面所示的 xaml 代码...
如何做我在运行时在视图后面的代码中执行上述操作。
另外,我想将动态生成的列表框的数据上下文分配给与当前绑定到我的视图的视图模型不同的视图模型。
基本上,我有一个全景图,每个全景项目都是动态创建的,每个全景项目都有一个列表框,该列表框将通过中继命令绑定到视图模型
I have a page with
namespaces:
xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
xaml at design time:
<ListBox x:Name="ItemGroupsList" ItemsSource="{Binding ItemGroups}" Height="496"
SelectedItem="{Binding SelectedItemGroup, Mode=TwoWay}" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="SelectionChanged">
<GalaSoft_MvvmLight_Command:EventToCommand
x:Name="SelectionChangedEvent"
Command="{Binding GoToEditItemGroupCommand, Mode=OneWay}"
PassEventArgsToCommand="True"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
In the code, I am generating multiple listboxes at run time and would like to be able to bind to a relaycommand on the viewmode like the above xaml code shown above...
How do I do the above at run time in the code behind of the view.
Also, I would like to assign the datacontext for the dynamically generated listboxes to different view models than the one currently bound to my view.
Basically, I have a panaroma and with each panaroma items being created dynamically with each panaroma item having a listbox that will be bound to a viewmodel with relaycommands
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意:另请参阅相关发布。
本文介绍了如何从代码隐藏中附加行为。
但是,我坚决不建议您走这条路,除非您有令人信服的需要这样做。如果您在
ViewModel
中使用此方法,您将失去所有可测试性,因为它会生成与View
高度耦合的ViewModel
,并且没有它,事实就无法生存。 (旁注:因此,将事件参数返回到ViewModel
也不是一个好习惯,使用CommandParameter
返回>DataContext
(如果需要)。通常,您可以使用 MVVM 以另一种方式归档您的目标,本文的其余部分对此进行了描述。
首先,您不需要使用
Command
来获取选定的属性,也不需要获取该属性已更改的通知。通常的模式是将列表框的SelectedItem
绑定到ViewModel
中的属性。现在,您可以使用PropertyChanged
事件来跟踪此属性何时发生更改。其次,使用模板生成列表框并设置其样式。当您只需要在选择项目时显示它们时,请使用
BooleanToVisibility
(示例请参见此处转换器,并将子列表框的Visibility
属性绑定到您的ViewModel
上的属性转换器(不是我的示例)。该示例创建一个 ListBox,其
ItemsSource
绑定到ViewModel
的 Items 属性。它还创建一个ContentControl<。 /code> 将其
DataContext
绑定到ViewModel
的SelectedItem
,然后ContentControl
再次包含。代码>列表框其中
DataContext
绑定到ItemViewModel
的SubItem
属性。MainViewModel
生成要在设计时显示的测试数据。帖子的其余部分显示了一个示例:
1. 主页的 XAML(除了):
2.主视图模型
3. ItemView模型
公共类ItemViewModel:ViewModelBase
{
#region [Name]
Edit 2
Panorama
控件的模板可以在 此处。Note: See also related post.
This article describes how you can attach behaviours from code behind.
However, I firmly would not advise you to go down this route unless you have a compelling need to do so. If you use this approach in your
ViewModel
, you will loose all testability as it generates aViewModel
that is highly coupled to theView
, and in fact cannot live without it. (Side note: for this reason it is also not good practise to return the event args to theViewModel
useCommandParameter
instead to return theDataContext
if needed).Normally you can archive your goal using MVVM in another manner, and the rest of the post describes this.
First, you do not need to use a
Command
to get the selected property, nor to get the notification that this property has changed. The usual pattern for this is that you bind theSelectedItem
of your list box to a property in yourViewModel
. Now you can use thePropertyChanged
event to track when this property changed.Second, use templates to generate and style your listboxes. When you need to only show them when the item is selected use a
BooleanToVisibility
(Sample see here converter and bind the sub-listboxes'Visibility
property to a property on yourViewModel
using the converter (not my the sample).The sample creates a ListBox that has its
ItemsSource
bound to the Items property of theViewModel
. It further creates aContentControl
that has itsDataContext
bound to theSelectedItem
of theViewModel
. TheContentControl
then contains again aListBox
where theDataContext
is bound to theSubItem
property of theItemViewModel
. TheMainViewModel
generates test data to be shown at design time and run time.The rest of the post shows an sample:
1. XAML of main page (except):
2. The Main View Model
3. The ItemView Model
public class ItemViewModel : ViewModelBase
{
#region [Name]
Edit 2
The templating of the
Panorama
control can be found here.