【问题标题】:MVVM WPF bindings with RelativeSourceMVVM WPF 与 RelativeSource 的绑定
【发布时间】:2012-06-07 12:32:46
【问题描述】:
<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 模式。我想绑定到 ImageClick 属性。

我之前能够做到这一点,但由于我在数据模板中移动了按钮,因此无法绑定到该属性。

我认为我需要使用相对源进行绑定,但我真的不确定该怎么做。

在这方面的帮助将是惊人的。

谢谢。

【问题讨论】:

标签: c# .net wpf mvvm-light


【解决方案1】:

你可以试试下面的。

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

我想它会对你有所帮助..

【讨论】:

  • 答案很对,除了 type: x:Type YourViewModel 应该改成 x:Type YourView
猜你喜欢
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
相关资源
最近更新 更多