我有以下XAML代码,用户在每次按下Enter键时都会提交内容。 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>

在我的MainViewModel.cs中定义了FieldsTextProperty属性:
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");
  }

该代码获取当前对象,并将文本框中的内容添加到该对象中。

这不起作用,我看不到为什么?
当我按下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中。我的老 friend 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>

最佳答案

我有一个预感..也许绑定(bind)发生时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}}}">

10-08 03:07