2012-06-07 127 views
0
<DataTemplate DataType="{x:Type local:TestModel}">  
    <Button Content="Button" " Margin="0"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Click"> 
       <mvvm:EventToCommand 
        Command="{Binding ImageClick, Mode=OneWay}" 
        MustToggleIsEnabledValue="True"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </Button> 
</DataTemplate> 

大家好,MVVM WPF绑定与的RelativeSource

上面我有使用数据模板上特定的数据类型转换数据模板。我正在使用mvvm模式。我想要绑定到ImageClick属性。

我之前可以做到这一点,但由于我已经移动了数据模板中的按钮,我无法绑定到该属性。

我想我需要使用相对来源的绑定,但我真的不确定如何做到这一点。

关于此的帮助将会很棒。

谢谢。

+2

也许这个答案将有助于http://stackoverflow.com/questions/10596094/bind-command-from-business-object-to-view- in-mvvm/10596243#10596243 – Zabavsky

+0

看起来不错,取决于ImageClick命令的位置。我们可以看到视图模型吗? – Phil

回答

1

您可以尝试以下操作。

<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type YourViewModel}}, Path=DataContext.ImageClick}" /> 

我认为这将帮助你..

+2

答案是非常正确的,除了类型:x:类型YourViewModel应改为x:Type YourView –