【问题标题】:Sending Content of ContentPresented as Attached Command Parameter将 ContentPresented 的内容作为附加的命令参数发送
【发布时间】:2014-01-22 12:27:38
【问题描述】:

在MVVM demo app by Josh Smith 中,当您尝试通过那个标签(这不是当前选择的标签)上的关闭按钮关闭标签时,它会不关闭,而是关闭选定的选项卡。

如何修改代码,以便当我单击一个选项卡项的关闭按钮而不是当前选择的选项卡时,它会关闭右侧的选项卡?

注意,我尝试过使用AttachedProperties 和以下内容:

AttachedCommand:CommandBehavior.Event="Click"
AttachedCommand:CommandBehavior.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CloseWorkspaceCommand}"
AttachedCommand:CommandBehavior.CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext}"

我传入CommandParameter,但这会获取托管视图的视图模型,而不是绑定到ContentPresenter 的视图模型。

感谢您的宝贵时间。

【问题讨论】:

  • 发布一些简化的示例:View/ViewModel。似乎是一件简单的事情......没有你的代码,很难说出你卡在哪里。有太多的可能性。我可以编写一些代码作为答案,但这是浪费时间——可能看起来不错,但对你没有任何帮助,这是底线(除了要点)!
  • 你试过 {Bindnig} 吗?
  • @ZSH,看好伙伴。这就是答案。如果您将其发布为与 为什么会起作用的答案,我将很乐意接受!非常感谢您的宝贵时间!
  • @denismorozov 非常感谢您抽出宝贵时间。由于代码太长,我没有发布完整的代码,并且认为熟悉 Josh Smiths 示例 MVVM 应用程序的人会知道我在说什么(大多数 MVVM WPF 人员都使用过这个示例)。再次感谢!

标签: c# wpf mvvm command


【解决方案1】:

尝试使用 this ,它将直接绑定到视图的数据上下文

AttachedCommand:CommandBehavior.CommandParameter="{Binding}"

我希望这会有所帮助。

【讨论】:

  • 我对其进行了编辑以使其更清晰,希望您不要介意:]。您是否还可以添加为什么这有效,因为我不清楚...再次感谢您的时间。
猜你喜欢
  • 2012-08-08
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多