0

I have a ListBox on a WPF form. I would like to display a list of string items horizontally. I have a Grid that holds my ListBox control.

When I run the form it displays the name of the encapsulating object: ProjectName.Folder.Category instead of the string object within it.

ViewModelLocator

public CategoryViewModel CategoryViewModel
{
    get
    {
        if (categoryviewModel == null)
        {
            categoryviewModel = new CategoryViewModel();
            categoryviewModel.ListData.Clear();
            categoryviewModel.ListData.Add(new Category { MyCategory = "new categroy1" });
            categoryviewModel.ListData.Add(new Category { MyCategory = "new categroy2" });
            categoryviewModel.ListData.Add(new Category { MyCategory = "new categroy3" });
            categoryviewModel.ListData.Add(new Category { MyCategory = "new categroy4" });
            categoryviewModel.ListData.Add(new Category { MyCategory = "new categroy5" });
            categoryviewModel.ListData.Add(new Category { MyCategory = "new categroy6" });
        }
        return categoryviewModel;
    }
}

Model

class Category
{
    public String MyCategory { get; set; }
}

MainPage.xaml

<Grid>
    <my:featureControl HorizontalAlignment="Left" 
                       x:Name="featureControl1" 
                       VerticalAlignment="Top" Height="332" 
                       Loaded="featureControl1_Loaded" />
</Grid>

Control.xaml

<UserControl x:Class="AmebaPrototype.UI.Longlist.CategoryControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="1280">
<Grid DataContext="{Binding Source={StaticResource viewModelLocator},Path=CategoryViewModel}">

    <ListBox Height="300" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="1280" ItemsSource="{Binding ListData}" >
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>        
    </ListBox>
</Grid>

2 Answers 2

3

You need to set the DisplayMemberPath in your ListBox

Sign up to request clarification or add additional context in comments.

1 Comment

...or specify an ItemTemplate.
0

This seemed to have done the trick.

<DataTemplate x:Key="ListBoxTemplate">
    <TextBlock x:Name="black"  Text="{Binding MyCategory}"/>
</DataTemplate>

2 Comments

It will work, but just putting DisplayMemberPath="MyCategory" on your ListBox line is a lot simpler :)
Yep just did that, Thanks @DeanChalk . I will accept your answer in a couple mins when it lets me :)

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.