Rafal
Rafal

Reputation: 91

WPF listbox code behind

I need a little help with my program. I done simple program for adding Categories deleting them and saving. The program look something like this

link

And all of the options work but when I click "Add Category" I can't see names of my categories in listbox and I don't know why so if someone can help me fix this will be great.

XAML code:

<Grid>
    <TextBox Name="CategoryTextBox"  HorizontalAlignment="Left" Height="23" Margin="25,26,0,0" TextWrapping="Wrap" Text="Category Name" VerticalAlignment="Top" Width="120"/>
    <Button Click="Button_Click" Content="Add Category" HorizontalAlignment="Left" Margin="171,26,0,0" VerticalAlignment="Top" Width="111"/>
    <Button Click="Button_Click_1" Content="Save" HorizontalAlignment="Left" Margin="171,104,0,0" VerticalAlignment="Top" Width="111"/>
    <Button Click="Button_Click_2" Content="Delete Category" HorizontalAlignment="Left" Margin="171,64,0,0" VerticalAlignment="Top" Width="111"/>
    <ListBox Name="CategoryListBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="195" Margin="25,64,0,0" VerticalAlignment="Top" Width="120">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding CategoryName}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

CategoryList.cs Code:

public partial class CategoryList
{
        public string CategoryName { get; set; }

}

C# Code:

public void Button_Click(object sender, RoutedEventArgs e)
{
          List<CategoryList> cat = new List<CategoryList>();
          string text = CategoryTextBox.Text;
          cat.Insert(0, new CategoryList { CategoryName = text });
}

Upvotes: 2

Views: 2602

Answers (1)

mrsargent
mrsargent

Reputation: 2442

First, looks like you don't have your data context. As you said you are using the code-behind so in the constructor I would do something like this

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        SomeList = new ObservableCollection<CategoryList>();         
    }

    public ObservableCollection<CategoryList> SomeList { get; set; }
}

Second, you should change your collection type to an ObservableCollection. When you use ObservableCollection it will automatically update the binding as it uses the collection changed event.

Finally, you can set your Listbox itemsource to your ObservableCollection property in the codebehind.

<ListBox Name="CategoryListBox" ItemsSource="{Binding SomeList}" HorizontalAlignment="Left" Height="195" Margin="25,64,0,0" VerticalAlignment="Top" Width="120">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding CategoryName}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Then when you add data to the ObservableCollection with your AddCategory button event it will update the collection and will also update your listbox.

Upvotes: 2

Related Questions