我创建了一个包含 ListView 的用户控件。当用户更改嵌套文本框的文本(使用 MVVM Light)时,我想添加一个 RelayCommand:
<UserControl xmlns:my="clr-namespace:UI.View" x:Class="UI.View.MontureView"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" >
<ListView ItemsSource="{Binding Path=Monture}" Margin="0,39,0,95" Height="600" HorizontalAlignment="Center">
<ListView.View>
<GridView>
<GridViewColumn Header="Qte" Width="50" >
<GridViewColumn.CellTemplate >
<DataTemplate>
<TextBox Text="{Binding Path=Qte}" Width="40" TextAlignment="Right" Name="a">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged" >
<cmd:EventToCommand Command="{Binding MontureViewModel.MyProperty}" CommandParameter="{Binding ElementName=a}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</UserControl>
在我的虚拟机中:(我删除了部分代码)
namespace UI.ViewModel
{
public class MontureViewModel : ViewModelBase
{
public MontureViewModel()
{
MyProperty = new RelayCommand<TextBox>(e =>
{
MessageBox.Show("test");
});
}
public RelayCommand<TextBox> MyProperty { get; set; }
}
}
我尝试在未嵌套到 DataTemplate (在 ListView 之外)中的 TextBox 上添加一个事件,并且它可以工作。我认为当我进入 DataTemplate 时我必须修改代码。
任何想法 ?