在WPF 4.0上具有MVVM模式的可编辑ItemsControl DataTemplate [英] Editable ItemsControl DataTemplate with MVVM pattern on WPF 4.0
问题描述
在 ItemsControls (或我的视图或viewModel)中定义项目时遇到问题.可能是我不了解mvvm,但这是问题所在.我将 DataTemplate 定义为
I''ve got problem to define items in my ItemsControls (or in my view or viewModel). May be I don''t know something about mvvm, but here is the question. I have DataTemplate defined as
<DataTemplate>
<Expander>
<Expander.Header>
<StackPanel>
<TextBlock Margin="2" Text="{Binding Name}" />
<TextBlock>
<Hyperlink>
<Run Text="Delete group" />
</Hyperlink>
</TextBlock>
</StackPanel>
</Expander.Header>
<Expander.Content>
blah-blah-blah
</Expander.Content>
</Expander>
</DataTemplate>
我有很多扩展器,每个都有超链接.我希望超链接将执行其必须执行的操作-删除组-即删除相应的 Expander .我已经读过关于使用 Visual 和 Logical - TreeHelper 在 DataTemplate 上搜索的信息,但这是不好的操作-它是不是mvvm.我知道我需要为 Hyperlink 写一些 Commond ,但是我该如何检索需要删除的 Expander 并满足mvvm的要求-多数民众赞成在这个问题.
任何建议,任何建议...
I have a lot of Expanders, each of them have Hyperlink. And I want Hyperlink will do what it must do - delete group - i.e. remove corresponding Expander. I have read about searching on DataTemplate with Visual and Logical - TreeHelper, but it is bad action - it is not mvvm. I know that I need to wrote some Commmand for Hyperlink, but how can I retrive Expander, which I need to delete, and satisfy mvvm - thats the question.
Any advice, any suggestion...
推荐答案
你好
您应该像上面提到的那样创建一个命令.
您可以通过多种方式访问命令",例如以下示例:
Hello
You should make a Command, like you mentioned.
You can access the Command in many ways, like this for one example:
<Hyperlink Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=DataContext.RemoveExpanderCommand, Mode=OneWay}" CommandParameter="{Binding Path=.}">
<Run Text="Delete group" />
</Hyperlink>
本示例将查找第一个Window祖先.如果有UserControl,则将其更改为"UserControl";如果具有TemplatedControl,则将其更改为控件名称.
注意:有很多检索命令的方法.
您也可以使用:
This example will fint the first Window ancestor. Change it to "UserControl" if you have a UserControl, or to the control name if you have a TemplatedControl.
Note : The are lots of ways to retrieve the command.
You can also use :
Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent} (...)
直接访问"ItemsControl".
您也可以使用:
to access the "ItemsControl" directly.
You can also use :
Command="{Binding ElementName=MyElementName (...)
通过x:Name属性访问特定控件.
我用Path =添加了"CommandParameter".将当前项目发送到Command,因为"ItemsControl"不支持"SelectedItem".
然后,我从列表中删除该项目.
to access a specific control by the x:Name property.
I added "CommandParameter" with Path=. to send the current item to the Command, because the "ItemsControl" does not support "SelectedItem".
Then I remove the item from the list.
private void doRemoveExpander(MyClass param)
{
this.Items.Remove(param);
}
项目"是ObservableCollection< Myclass>.其中包含要在列表中查看的项目.它在ItemsControl的"ItemsSource"中给出.
希望能有所帮助:-)
"Items" is a ObservableCollection<Myclass> which holds the items to view in the list. It is given in the "ItemsSource" for the ItemsControl.
Hope that helped a little :-)
这篇关于在WPF 4.0上具有MVVM模式的可编辑ItemsControl DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!