简体   繁体   English

WPF列表框代码后面

[英]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 该程序看起来像这样

链接

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: XAML代码:

<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: CategoryList.cs代码:

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

}

C# Code: C#代码:

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 });
}

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. 其次,您应该将集合类型更改为ObservableCollection。 When you use ObservableCollection it will automatically update the binding as it uses the collection changed event. 使用ObservableCollection时,它将使用集合更改事件自动更新绑定。

Finally, you can set your Listbox itemsource to your ObservableCollection property in the codebehind. 最后,您可以在后面的代码中将Listbox itemsource设置为ObservableCollection属性。

<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. 然后,当您使用AddCategory按钮事件将数据添加到ObservableCollection时,它将更新集合并且还将更新列表框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM