永远不会触发EventToCommand - MVVM

时间:2014-11-25 14:14:47

标签: c# wpf mvvm mvvm-light

我有以下XAML代码,用户每次按下输入按钮时都会提交内容。 EventToCommand调用ViewModel中的AddFieldCommand方法。

<TextBox x:Name="txtFields" Text="{Binding FieldsTextProperty, UpdateSourceTrigger=PropertyChanged}" Height="23" TextWrapping="NoWrap" Background="#FFCBEECD" AcceptsReturn="False" >
                <i:Interaction.Triggers>
                    <iex:KeyTrigger Key="Enter">              
                        <cmd:EventToCommand Command="{Binding DataContext.AddFieldCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
                    </iex:KeyTrigger>
                </i:Interaction.Triggers>
</TextBox>

属性FieldsTextProperty在我的MainViewModel.cs中定义:

public string FieldsTextProperty { get; set; }

AddFieldCommand的我的ICommand看起来像这样:

public ICommand AddFieldCommand { get; private set; } 

并初始化为:

AddFieldCommand = new RelayCommand<KeyEventArgs>(AddField);

AddField方法如下所示:

 public void AddField(KeyEventArgs e)
 {
        FrameworkElement classDataVisualElement = (FrameworkElement)e.KeyboardDevice.Target;
        ClassData classDataModel = (ClassData)classDataVisualElement.DataContext;

        classDataModel.Fields.Add(FieldsTextProperty);

        FieldsTextProperty = "";

        RaisePropertyChanged("Fields");
  }

代码获取当前对象,并将TextBox中的内容添加到对象中。

这不行,我看不出原因? 当我按Enter键时,没有任何事情发生,FieldsTextPropertyAddField从未被调用

编辑: 我的输出窗口中出现以下错误: 'FieldsTextProperty' property not found on 'object' ''ClassData' (HashCode=46358046)'. BindingExpression:Path=FieldsTextProperty; DataItem='ClassData' (HashCode=46358046); target element is 'TextBox' (Name='txtFields'); target property is 'Text' (type 'String')

编辑2: 经过多次测试 - 似乎问题出现在FieldsTextProperty中。我的老朋友Console.Writeline(“AddField Method”)永远不会令人失望;)

编辑3: 完整(几乎)XAML代码:

<UserControl ..... >

<Grid>
    <TextBlock Text="Class Name" Background="#FF96BB96" Foreground="Black" IsHitTestVisible="True">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDown">
                <cmd:EventToCommand Command="{Binding DataContext.MouseDownClassDataCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseMove">
                <cmd:EventToCommand Command="{Binding DataContext.MouseMoveClassDataCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseUp">
                <cmd:EventToCommand Command="{Binding DataContext.MouseUpClassDataCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBlock>

    <StackPanel Opacity="{Binding DataContext.ModeOpacity, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
        <Border BorderBrush="#FF53855E" BorderThickness="1" Height="25"/>

        <!-- FIELDS ELEMENTS-->
        <Grid>
            <TextBox x:Name="txtFields" Text="{Binding FieldsTextProperty, UpdateSourceTrigger=PropertyChanged}" Height="23" TextWrapping="NoWrap" Background="#FFCBEECD" AcceptsReturn="False" >
                <i:Interaction.Triggers>
                    <iex:KeyTrigger Key="Enter">              
                        <cmd:EventToCommand Command="{Binding DataContext.AddFieldCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
                    </iex:KeyTrigger>
                </i:Interaction.Triggers>
            </TextBox>


            <TextBlock IsHitTestVisible="False" Text="Insert Fields.." VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,0,0,0" Foreground="DarkGray">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Text, ElementName=txtFields}" Value="">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </Grid>

        <DataGrid x:Name="PropertiesControl1" Height="auto" ItemsSource="{Binding ClassDatas}" HeadersVisibility="None" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="True">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Fields}" Header="" Width="*" IsReadOnly="True"/>
            </DataGrid.Columns>
        </DataGrid>

        <!-- More of the same code...-->

    </StackPanel>

</Grid>

</UserControl>

2 个答案:

答案 0 :(得分:0)

我有预感..当绑定发生时,Command可能为null 试试:

   private ICommand _addFieldCommand; 
   public ICommand AddFieldCommand 
   {
       get
       {
            if(_addFieldCommand == null)
                _addFieldCommand = new RelayCommand<KeyEventArgs>(AddField);
            return _addFieldCommand;  
       }
   } 

XAML:

     <UserControl DataContext"={Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">

答案 1 :(得分:0)

尝试添加:

AddFieldCommand.CanExecute = true;
初始化命令后