简体   繁体   English

在ItemTemplate选择上添加命令

[英]add command on ItemTemplate selection

This is a pretty easy question I think , but I can't find the answer. 我认为这是一个非常简单的问题,但我找不到答案。 I have an itemtemplate defined into a datatemplate. 我有一个定义为datatemplate的itemtemplate。 When an item is selected, I wanna trigger a command to select the name of my element and apply it somewhere else. 当选择一个项目时,我想触发一个命令以选择我的元素的名称并将其应用到其他地方。 For the moment the MouseDown event doesn't accept my command. 目前,MouseDown事件不接受我的命令。

<ListView Margin="4" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=ExistingStateInfos, ElementName=Window}"
                  SelectedItem="{Binding Path=SelectedStateInfo, ElementName=Window}" x:Name="statinfoListview">
            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type States:StateInfo}">
                    <TextBlock Text="{Binding Path=Name}" MouseDown="{x:Static MyWindow.ApplyStateInfoNameToStateCommand}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

You can't set a command directly to an event handler. 您不能直接将命令设置给事件处理程序。

Use EventToCommand from the MVVM LightToolkit 使用MVVM LightToolkit中的 EventToCommand

<DataTemplate DataType="{x:Type States:StateInfo}">
    <TextBlock Text="{Binding Path=Name}">
       <i:Interaction.Triggers>
         <i:EventTrigger EventName="MouseDown">
            <Command:EventToCommand Command="{Binding YourCommand, Mode=OneWay}" />
         </i:EventTrigger>
       </i:Interaction.Triggers>
    </TextBlock>
 </DataTemplate>

In fact ot was bulshit from me, I just add to do 实际上,这是我的废话,我只是想做

<TextBlock Text="{Binding Path=Name}" MouseDown="{x:Static ApplyStateInfoNameToState_Click}" />

Sorry it's friday. 对不起,这是星期五。 Have a nice weekend :) 周末愉快 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM