【发布时间】:2014-11-25 14:14:46
【问题描述】:
我有以下 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 并且 FieldsTextProperty 和 AddField 永远不会被调用时,什么也没有发生
编辑:
我的输出窗口中出现以下错误:
'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>
【问题讨论】:
-
任何绑定错误?查看您的输出窗口 AddFieldCommand ,我猜绑定发生时它为空。
标签: c# wpf mvvm mvvm-light