【问题标题】:using an event to command trigger inside a style在样式内使用事件来命令触发器
【发布时间】:2014-06-26 09:54:27
【问题描述】:

我正在尝试使用 Mvvm Light 工具包将事件绑定到样式中的命令。

我目前有风格:

<Style 
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:controls='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' 
xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
xmlns:ig='http://schemas.infragistics.com/xaml'
xmlns:i='http://schemas.microsoft.com/expression/2010/interactivity'
xmlns:Command='clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4'
TargetType='ig:EventSpan'>
      <Setter Property='Template'>
           <Setter.Value>
                <ControlTemplate TargetType='ig:EventSpan'>
                      <Grid Margin='0,2,0,2'>
                            <i:Interaction.Triggers>
                                  <i:EventTrigger EventName='MouseEnter'>
                                        <Command:EventToCommand Command='{Binding EventSpan_MouseEnter1}' CommandParameter='{Binding RelativeSource={RelativeSource AncestorType={x:Type ig:EventSpan}}}'/>
                                  </i:EventTrigger>
                                  <i:EventTrigger EventName='MouseLeave'>
                                        <Command:EventToCommand Command='{Binding EventSpan_MouseLeave1}' CommandParameter='{Binding RelativeSource={RelativeSource AncestorType={x:Type ig:EventSpan}}}'/>
                                  </i:EventTrigger>
                             </i:Interaction.Triggers>
                       <Rectangle RadiusX='0' RadiusY='0' Fill='{TemplateBinding Fill}' Stroke='{TemplateBinding Stroke}' StrokeThickness='0' Height='0' Margin='0, 0, 0, 0' VerticalAlignment='Top' />
                       </Grid>
                 </ControlTemplate>
           </Setter.Value>
       </Setter>
</Style>

后面的代码示例是:

private RelayCommand<string> _eventSpan_MouseEnter;
public RelayCommand<string> EventSpan_MouseEnter1
{
    get
    {
        return _eventSpan_MouseEnter
            ?? (_eventSpan_MouseEnter = new RelayCommand<string>(
                                  sender =>
                                  {
                                      MouseOverLayer = sender;
                                  }));
    }
}

但是这个命令永远不会触发,我不知道为什么?

PS。在样式中引用库的原因是因为此样式是从后面的代码动态加载的。这是我目前正在尝试转换为 mvvm 的遗留代码。

【问题讨论】:

    标签: c# wpf mvvm mvvm-light


    【解决方案1】:

    所以我之前绑定的方式有两个问题。首先,主要原因是样式没有数据上下文。并且显然没有从实现样式的模板继承数据上下文。并且命令参数不正确。

    CommandParameter='{Binding RelativeSource={RelativeSource AncestorType={x:Type ig:EventSpan}}}'
    

    被绑定到一个需要字符串参数的relaycommand,因此崩溃了。因此正确的命令参数是

    CommandParameter='{Binding Path=EventEntry.Series.Tag, RelativeSource={RelativeSource AncestorType={x:Type ig:EventSpan}}}'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 2013-10-08
      • 2012-03-28
      • 2012-11-13
      • 2014-05-02
      相关资源
      最近更新 更多