0

i have a problem in WPF application with binding data in ListBox.

Here is me xaml code:

<Window x:Class="DatabaseBoozeWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DatabaseBoozeWpf"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="625">
<Grid>
    <ListBox
             Margin="10,124,0,10"
             ItemsSource="{Binding Boozes}" 
             HorizontalAlignment="Left"
             ScrollViewer.VerticalScrollBarVisibility="Visible"
             ItemTemplate="{Binding Boozes}"

             Width="233">

    </ListBox>

</Grid>

But if I open the program, it will show this kind on text. It should output the list of products. enter image description here

5
  • 3
    ItemTemplate="{Binding Boozes}" makes no sense. Declare an ItemTemplate in XAML. Start reading here: Data Templating Overview. Commented Dec 14, 2017 at 8:34
  • see here wpf-tutorial.com/listview-control/… Commented Dec 14, 2017 at 8:36
  • Youc an just remove the ItemTemplate and override the ToString() Method of your boozes :) Commented Dec 14, 2017 at 8:40
  • @TobiasTheel Sure, but that's not the "WPF way". Commented Dec 14, 2017 at 9:07
  • @Clemens True, but it's working. Because of that reason i upvoted the answer of Sajeetharan Commented Dec 14, 2017 at 9:39

1 Answer 1

2

You should have an ItemTemplate with a DataTemplate with elements that bind to the properties of the item class.

<ListBox ItemsSource="{Binding Boozes}" ...>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding YourProperty}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
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.