简体   繁体   中英

WPF - Interaction Trigger not working in Listbox

I am trying to add a SelectionChanged interaction trigger to a ListBox in WPF so i can route the event to a command, but for some reason it's not working.

Here is my code

<Border Background="Transparent">
  <ListBox Name="MyListBox"
           ScrollViewer.HorizontalScrollBarVisibility="Disabled"
           SelectedValue="A"
           SelectedValuePath="Content">
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding MyCommand}"
                               CommandParameter="{Binding ElementName=MyListBox, 
                                                          Path=SelectedIndex}" />
      </i:EventTrigger>
    </i:Interaction.Triggers>
    <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
        <WrapPanel IsItemsHost="True" />
      </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBoxItem>A</ListBoxItem>
    <ListBoxItem>B</ListBoxItem>
  </ListBox>
</Border>

I guess i am doing something wrong here.

You should just bind the SelectedIndex to a property in your DataContext, which leads to simplier code :

<Border Background="Transparent">
    <ListBox Name="MyListBox" 
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
             SelectedValue="A" SelectedValuePath="Content"
             SelectedIndex="{Binding MyIndexProperty}">
           <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
               <WrapPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBoxItem >A</ListBoxItem>
        <ListBoxItem >B</ListBoxItem>
    </ListBox>
</Border>

Your code works fine. All you need is to provide a suitable view model, eg

Note: using MVVM Light

public class TestViewModel : ObservableObject
{
    public TestViewModel()
    {
        this.MyCommand = new RelayCommand<int>(i => Debug.WriteLine(i));
    }

    public RelayCommand<int> MyCommand { get; private set; }
}

Your Xaml with hard coded view model

<Window.DataContext>
    <my:TestViewModel/>
</Window.DataContext>
<Border Background="Transparent">
    <ListBox Name="MyListBox" 
    ... etc
    // This is a property on a GalaSoft MVVMLIght ViewModel

    /// <summary>
    ///   ThemeInfo of the current active theme
    /// </summary>
    public String ActiveTheme
    {
        get
        {
            if (activeTheme == null)
            {
                activeTheme = Properties.Settings.Default.Default_App_Theme;
            }
            return activeTheme;
        }
        set
        {
            if (activeTheme == value)
            {
                return;
            }

            var oldValue = activeTheme;

            activeTheme = value;

            // Update bindings

            RaisePropertyChanged(ActiveThemePropertyName,    oldValue, value, true);

            if (value != null)
            {


                     if (this.SwitchThemeCommand.CanExecute(value))
                        this.SwitchThemeCommand.Execute(value); 
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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