ListBox SelectionChanged不会被触发

时间:2015-07-08 09:48:44

标签: c# wpf xaml

我有一个像这样的ListBox

 <ListBox Grid.ColumnSpan="4"
             Grid.Column="1"
             Grid.Row="7"
             Grid.RowSpan="2"
             Style="{DynamicResource ProductListBoxStyle}"
             ItemTemplate="{DynamicResource ProductItemTemplate}"
             ItemsSource="{Binding RelatedProducts}"
             ItemsPanel="{DynamicResource ProductListItemsPanelTemplate}"
             SelectedItem="{Binding SelectedProduct,Mode=TwoWay}"
             Padding="-12,0,0,0"
             IsHitTestVisible="True"
             ItemContainerStyle="{DynamicResource ProductListItemContainerStyle}"
             x:Name=MyListBox >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>

当我再次点击同一项时SelectionChanged事件未触发。

所以我在我的DataTemplate中添加了一个Button,就像这样

   <DataTemplate x:Key="ProductItemTemplate">
        <Button Command="{Binding DataContext.SelectionChangedCommand,ElementName=MyListBox}">
            <StackPanel Width="240">
                <Grid Width="Auto"
                      Height="Auto"
                      VerticalAlignment="Stretch"
                      Background="White">
                    <Image Source="/Assets/ProductCardBg.png"
                           Width="Auto"
                           Height="Auto" />
                    <Image Source="{Binding ThumbImage}"
                           Width="207"
                           Height="216" />
                </Grid>
                <TextBlock Text="{Binding Name}"
                           Margin="0,5,0,0"
                           Style="{StaticResource SWMRegularTextBlockStyle}"
                           FontSize="{StaticResource VerySmallFontSize}" />
                <TextBlock Text="{Binding Description}"
                           Style="{StaticResource SWMLightTextBlockStyle}"
                           FontSize="{StaticResource SmallFontSize}" />
                <TextBlock Text="{Binding Price}"
                           Style="{StaticResource SWMRegularTextBlockStyle}"
                           FontSize="{StaticResource SmallFontSize}" />

            </StackPanel>
        </Button>
    </DataTemplate>

但我的问题是每当我选择项目时都不会执行命令。我需要单击StackPanel的Outside,然后只执行命令。但是每当我点击Item时我都需要执行命令。

请有人帮助我,提前致谢。

5 个答案:

答案 0 :(得分:1)

如果我理解正确,您可以使用MouseClick事件来实现它:

<i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding MouseClick}" CommandParameter="{Binding ElementName=MyListBox ,Path=SelectedItem}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>

并在后面的代码中:

void MyListBox_MyListBox(Object sender, MouseEventArgs e)
{
 //Your code
}

如果你想使用命令,而不是背后的代码做类似的事情:

创建一个名为BaseCommand的类:

    public class BaseCommand : ICommand
        {
            protected Func<object, bool> _canExecute;
            protected Action<object> _execute;

            public BaseCommand()
            {
            }

            public BaseCommand(Func<object, bool> canExcecute, Action<object> execute)
            {
                _canExecute = canExcecute;
                _execute = execute;
            }
            public bool CanExecute(object parameter)
            {
                return _canExecute(parameter);


 }
        public void Execute(object parameter)
        {
            _execute(parameter);
        }
        public event EventHandler CanExecuteChanged;
    }

 public bool CanExecute(object parameter)
 {
     return _canExecute(parameter);
 }

public void Execute(object parameter)
 {
      _execute(parameter);
 }
你在ViewModel中的

private BaseCommand _mouseClick;
public BaseCommand MouseClick
{
   get
   {
     if (null == _mouseClick)
                {
                   _mouseClick = new BaseCommand(CanExecuteCommand, OpenDocument);
                }
                return _open;
   }
}

 private bool CanExecuteCommand(object obj)
        {
            return true; //for example
        }

 public void MouseClickCommand(object o)
{
//do something
}

这样,每当您选择一个项目时,它都会激活该命令。

答案 1 :(得分:0)

试试这个,

 <Button Command="{Binding DataContext.SelectionChangedCommand, RelativeSource={RelativeSource AncestorType=ListBox}}">

如果你更改你的样式按钮会拉伸到listItem里面的任何地方然后按钮事件将触发。删除交互命令。

答案 2 :(得分:0)

在我的ViewModel

 private void ReatedItemSelected()
    {
        //After My code
        SelectedProduct = null;
    }

在我的代码之后如果我只是将SelectedProduct设置为null,再次selectionchanged事件触发,我不需要在我的dataTemplate中使用Button。 SelectedProduct绑定到UI并将模式设置为Twoway。

答案 3 :(得分:0)

为什么不在你房产的set部分调用命令?

private object _SelectedProduct;

public object SelectedProduct
{
    get { return _SelectedProduct; }
    set
    {
        _SelectedProduct = value;

        //Call a method in your view model here.
        DoSomethingUseful();
    }
}

答案 4 :(得分:0)

为什么不使用 MouseUp / Down事件如果你想在每次选择项目时都要执行某些事情(或者甚至再次选择相同的项目)!!

相关问题