gpt4 book ai didi

c# - EventToCommand 永远不会被触发 - MVVM

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:13 26 4
gpt4 key购买 nike

我有以下 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 从未被调用时没有任何反应

编辑:我的输出窗口中出现以下错误:在“对象”“ClassData”(HashCode=46358046) 上未找到“FieldsTextProperty”属性。绑定(bind)表达式:路径=字段文本属性; DataItem='ClassData' (HashCode=46358046);目标元素是 'TextBox' (Name='txtFields');目标属性是“文本”(类型“字符串”)

编辑 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)发生时,命令可能为空。
尝试:

   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}}}">

关于c# - EventToCommand 永远不会被触发 - MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27128996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com