0

I am having a problem, I have a datatable data with 3 columns(ID,NAME,QUANTITY), I want to bind it with a ListBox, and make ListBox shows values from column NAME and QUANTITY,otherwise when I double click in a selected item,it will send ID value, here is my XAML:

<ListBox  HorizontalAlignment="Left" Margin="6,0,0,0" Name="ListBox1" VerticalAlignment="Top" Height="600" Width="321">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ListBoxItem}">
                                <EventSetter Event="MouseDoubleClick" Handler="ListBox1Item_DoubleClick" />
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.Resources>
                            <DataTemplate x:Key="listBoxTemplate">
                                <DockPanel >
                                    <TextBlock FontWeight="Bold" Text="{Binding NAME}"
                  DockPanel.Dock="Left"
                  Margin="5,0,10,0" Width="100"/>

                                    <TextBlock Text="{Binding QUANTITY} "   Foreground="Green" FontWeight="Bold" />
                                </DockPanel>
                            </DataTemplate>
                        </ListBox.Resources>
                    </ListBox> 

Here is my code behind:

...
            ListBox1.ItemsSource = data.DefaultView;
            ListBox1.SelectedValuePath = "ID";
...

But it does not show anything, something wrongs? please help! thanks for reading this!

1 Answer 1

1

You need to set the ListBox.ItemTemplate. At the moment you are defining a template with a key, that template is not being used anywhere.

<ListBox.ItemTemplate>
    <DataTemplate>
        <DockPanel >
            <TextBlock Text="{Binding NAME}" FontWeight="Bold"
                       DockPanel.Dock="Left"
                       Margin="5,0,10,0" Width="100" />
            <TextBlock Text="{Binding QUANTITY}" FontWeight="Bold"
                       Foreground="Green" />
        </DockPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.