简体   繁体   中英

wpf ListBox Control: simple GetSelectedValue / item-value

List box is not binded just a Combobox replacement (values are exposed)

Xaml

  <ListBox SelectionChanged="LBX_AddTaskOptions_SelectionChanged"  HorizontalAlignment="Left" Margin="19,29,0,0" Name="LBX_AddTaskOptions" VerticalAlignment="Top" Width="125" FontWeight="Bold" Background="Beige">
                        <ListBoxItem Background="Beige" FontWeight="Bold" v>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="internet"></TextBlock>
                                <Image Source="Images\IE_BlackRed.png" Height="30"></Image>
                            </StackPanel>
                        </ListBoxItem>
                        <ListBoxItem Background="Beige" FontWeight="Bold">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="localFolder"></TextBlock>
                                <Image Source="Images\Folder_Black.png" Height="30"></Image>
                            </StackPanel>
                        </ListBoxItem>
                    </ListBox>

CodeBehind

    private void LBX_AddTaskOptions_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var SelItm = LBX_AddTaskOptions.SelectedItem.ToString();

        MessageBox.Show(Sel);

    }

i have searched for that question, though answers are only for complex issues as i am fresh .net Developer, i know all methods to extract DDL text/value i even made extentions , though couldn't figure how to do this simple value extraction

shouldn't it be simple ?

messageBox shows the name of control (:

This isn't quite the right approach for XAML. You don't want to list out the markup for each item -- instead, use an ItemTemplate to define how it should look, and use bindings to render the actual item:

<ListBox SelectionChanged="LBX_AddTaskOptions_SelectionChanged" Name="LBX_AddTaskOptions">
    <ListBox.ItemTemplate>
        <ListBoxItem Background="Beige" FontWeight="Bold" v>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" />
                <Image Source="Images\IE_BlackRed.png" Height="30" />
            </StackPanel>
        </ListBoxItem>
    </ListBox.ItemTemplate>
</ListBox>

Bind the ListBox ItemsSource to the model data itself (ie, the array of strings in this case). Now, eventually you'll probably want to use a view model, but you can also add the items from code behind on load:

string[] ListBoxItems = new string[] { "internet", "local folder" };
LBX_AddTaskOptions.ItemsSource = ListBoxItems;

This should result in SelectedValue giving you the correct value.


Footnote -- you could get the selected value using the markup you've written out in the question -- but it would be ugly and would defeat the whole purpose of XAML. You'd need to cast SelectedItem to a ListBoxItem , then get its child and cast that to a StackPanel, get its children, etc, you get the idea. And then, of course, if the markup changes at all, the code you just wrote is no longer valid.

The item that you are getting in your selected value is a ListBoxItem with a control inside it. If you want to extract the value like the text then you have to do this

private void LBX_AddTaskOptions_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var SelItm = LBX_AddTaskOptions.SelectedItem as ListBoxItem;
        var StackPanel = SelItm.Content as StackPanel;
        foreach (var child in StackPanel.Children)
        {
            if(child is TextBlock)
            {
                MessageBox.Show((child as TextBlock).Text);
            }
        }

}

You have to sort of dig into the control to get the actual text. There are a lot of ways to get the value but this is the pretty basic one.

Calling ToString() method will just convert the current object as a string which is a ListBoxItem.

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