简体   繁体   中英

How to capture Listbox selected item

I am using MVVM and my code is as follows

  <ListBox Grid.Row="0"
             x:Name="myListBox" 
             ItemsSource="{Binding Path=MyClass}"
             ItemTemplate="{StaticResource MyDataTemplate}"
             SelectedItem="{Binding Path=SelectedItem}"
             HorizontalContentAlignment="Stretch">    
    </ListBox>

The ItemTemplate contains TextBlock and Label

and in my ViewModel

    public object SelectedItem
    {

        get
        {
            return _SelectedItem;
        }
        set
        {
            _SelectedItem = value;
            //Perform My Command
        }
    }

Its only after double click I am able to select the item.How can I make it to single mouse left click? Is there any way to convert double click to single click ?

Try this. Add MouseUp event:

<ListBox Grid.Row="0"
         x:Name="myListBox" 
         MouseUp="myListBox_MouseUp"
         ItemsSource="{Binding Path=MyClass}"
         ItemTemplate="{StaticResource MyDataTemplate}"
         SelectedItem="{Binding Path=SelectedItem}"
         HorizontalContentAlignment="Stretch">
    </ListBox>

And in code behind:

private void myListBox_MouseUp(object sender, MouseButtonEventArgs e)
{

}

If you want to do this in MVVM You need to use Command for example

I'm not sure that this is what you want to, but this works for me:

<ListBox Grid.Row="6"
         x:Name="myListBox" 
         ItemsSource="{Binding Path=MyClassItems}"
         SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"
         HorizontalContentAlignment="Stretch">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <StackPanel Orientation="Horizontal">
                                <StackPanel Orientation="Horizontal">
                                    <Label Content="FooLabel: "/>
                                    <TextBlock Text="{TemplateBinding Content}"/>
                                </StackPanel>
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

You can add your codes to :

private void listBox1_Click(object sender, MouseEventArgs e)
        {
           //Codes : 
        }

Or you can raise this event when you double click in the ListBox :

private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        listBox1.Click+=listBox1_Click; 
    }

Update : If you want to do something after clicking on the item, actually when you click you are changing the index of selected item, then you're going to raise an event which called listBox1_SelectedIndexChanged . So all you have to do is add the codes in this event.

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Your Codes .....
    }

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