2

ListView を含むユーザー コントロールを作成しました。ユーザーがネストされた TextBox のテキストを変更したときに RelayCommand を追加したい (MVVM Light を使用):

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

私のVMには次のものがあります:(コードの一部を削除しました)

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 を使用する場合は、コードを変更する必要があると思います。

何か案が ?

4

1 に答える 1

0

CommandParameter="{Binding ElementName=a}" を次のように変更する必要があります。

CommandParameter="{Binding SelectedItem, ElementName=a}". これにより、CommandParameter が GridView の選択された項目にバインドされ、ElementName はバインディング内でバインドされているコントロールを設定します。

于 2012-09-05T10:16:04.553 に答える