C# 使用父级的 DataContext(WPF - 动态菜单命令绑定)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/998092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:10:53  来源:igfitidea点击:

Using the parent's DataContext (WPF - Dynamic Menu Command Binding)

c#wpfdynamicmenucommandbinding

提问by Jesus Rodriguez

I looked over this web and google and the solutions didn't work for me.

我查看了这个网络和谷歌,解决方案对我不起作用。

I have a command on the ViewModel of a UserControl. Well, The usercontrol have a ItemsControl binded to a ObservableCollection. Inside the DataTemplate of the ItemsControl.ItemTemplate I have a button and I want to use the command. I can't bind the command because inside the DataTemplate, the datacontext is not the ViewModel but an item of the ObservableCollection.

我在 UserControl 的 ViewModel 上有一个命令。好吧,用户控件有一个绑定到 ObservableCollection 的 ItemsControl。在 ItemsControl.ItemTemplate 的 DataTemplate 内,我有一个按钮,我想使用该命令。我无法绑定命令,因为在 DataTemplate 内部,数据上下文不是 ViewModel,而是 ObservableCollection 的一个项目。

The question is: How can I bind the button to the command if a lost the parent datacontext?

问题是:如果丢失了父数据上下文,我如何将按钮绑定到命令?

I think that this need to have an easy solution because I think that this is a common problem.

我认为这需要有一个简单的解决方案,因为我认为这是一个常见问题。

Imagine this sceneario:

想象一下这个场景:

You have a ListBox item with an observableCollection as the ItemsSource, so you are using a datatemplate inside the ListBox for every element in the collection. Well, you want to delete the selected item and you put a button in every row for that job. ?How do you do that?

您有一个带有 observableCollection 作为 ItemsSource 的 ListBox 项,因此您在 ListBox 内为集合中的每个元素使用了一个数据模板。好吧,您想删除所选项目,并在该作业的每一行中放置一个按钮。?你是怎样做的?

In MVP, I can do this in the click event of the button:

在 MVP 中,我可以在按钮的单击事件中执行此操作:

Button but = e.Source as Button;

if (but != null)
      Presenter.ActualNote = but.DataContext as Note;

In short. You send the datacontext of the row (the selected item) to the presenter.

简而言之。您将行(所选项目)的数据上下文发送给演示者。

But, how can I do it in the mvvm way? Because I need to use a command but I can't assign the command to the button because the button does know nothing about the ViewModel (where the command exists).

但是,我怎样才能以 mvvm 的方式做到这一点?因为我需要使用命令,但我无法将命令分配给按钮,因为按钮对 ViewModel(命令所在的位置)一无所知。

As you can see, the button has to exist inside the datatemplate, then the datacontext is not the ViewModel anymore.... There is why I need to access to the parent's DataContext, for access to the command.

如您所见,该按钮必须存在于数据模板中,然后数据上下文不再是 ViewModel ......这就是为什么我需要访问父级的 DataContext,以访问命令。

I hope that you understand my problem better.

我希望你能更好地理解我的问题。

Thank you.

谢谢你。

采纳答案by geofftnz

If you want a dirty, MVVM-breaking solution, then set the Tag="{Binding}" on the button and handle the Click event. In the event handler, call the command on your ViewModel.

如果你想要一个肮脏的、破坏 MVVM 的解决方案,那么在按钮上设置 Tag="{Binding}" 并处理 Click 事件。在事件处理程序中,调用 ViewModel 上的命令。

回答by Eddie Deyo

Use the binding below for your button's command:

将下面的绑定用于您的按钮命令:

{Binding DataContext.CommandName, 
         RelativeSource={RelativeSource FindAncestor, 
                         AncestorType={x:Type MyUserControl}}}

This will tell it to find your UserControl and use its DataContext.

这将告诉它找到您的 UserControl 并使用其 DataContext。

回答by Dmitry Tashkinov

RelativeSource works, but I don't think it's right to let controls to prowl across each other's properties. It is strange that button placed inside an item view does something with an outer data source rather than with the bound item. You might need to review the program code's design.

RelativeSource 有效,但我认为让控件在彼此的属性之间徘徊是不对的。奇怪的是,放置在项目视图中的按钮对外部数据源而不是绑定项目执行某些操作。您可能需要检查程序代码的设计。

回答by Dmitry Tashkinov

Ok, then what about modifying your data item class so that it has a property referencing to the whole model view?

好的,那么如何修改您的数据项类以使其具有引用整个模型视图的属性?

If your ItemsSource is of type ObservableCollection<DataItem>then modify DataItem type like this:

如果您的 ItemsSource 是类型,ObservableCollection<DataItem>则像这样修改 DataItem 类型:

public class DataItem
{
    public BusinessObject Value { get; set; }

    private ModelView modelView;

    public ModelView ModelView
    {
        get
        {
            return modelView;
        }
    }

    public DataItem(ModelView modelView)
    {
        this.modelView = modelView;
    }
}