2

I have a MainWindow bound to its mainViewModel. inside the MainWindow I have a usercontrol defind like this

<vm:StatPanel DockPanel.Dock="Right" DataContext="{Binding Source={StaticResource viewModel}}" Loaded="StatPanel_Loaded" />

inside that usercontrol I have a datagrid with buttons. The goal is when the buttons are clicked to change a datagrid on the MainWindow xaml. this is what my usercontrol datagrid looks like

<Button Content="{Binding Path=sector}" Command="{Binding Path=filterGridCommand}"></Button>

when I run the application I get the following error.

System.Windows.Data Error: 40 : BindingExpression path error: 'filterGridCommand' property not found on 'object' ''mdSectorDetail' (HashCode=42410114)'. BindingExpression:Path=filterGridCommand; DataItem='mdSectorDetail' (HashCode=42410114); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')

I am using a command relay that is located in the MainViewModel. My problem is I dont know how to reference that mainViewModel, i have tried several of the suggested solutions like the following

CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type }}}"

Please any suggestions will be helpful. Thank you.

4

2 回答 2

1

您可以使用snoop找出您的 Button 的 DataContext 是什么。我认为在你的情况下是错误的 DataContext。如果你把 UserControl 的所有代码都给我,我会给你写一个合适的数据表。

于 2012-05-24T19:47:42.667 回答
0

我为这些事情使用了一个空的“标记”界面。

public interface IMyCommandDataContextHelper {}

具有我想通过相对源到达的数据上下文的控件/窗口必须实现空接口。

 public partial class MainWindow : IMyCommandDataContextHelper 

然后我可以很容易地用相对源编写我的 xaml

{Binding Path=DataContext.filterGridCommand, RelativeSource={RelativeSource AncestorType={x:Type local:IMyCommandDataContextHelper}}}

ps:属性应该是PascalCase :)

public ICommand FilterGridCommand {get{...}}
于 2012-05-25T09:51:50.813 回答