简体   繁体   English

WPF列表框使用模板添加项目

[英]WPF Listbox add item with Template

I searched everywhere but I didn't found any good example or tutorial for this. 我到处搜索,但我没有找到任何好的例子或教程。 I have a listbox and I made an ItemTemplate for it: 我有一个列表框,我为它创建了一个ItemTemplate:

XAML : XAML:

  <ListBox x:Name="LB_Playlist" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="454" Margin="0,23,0,0" VerticalAlignment="Top" Width="264" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0" ItemTemplate="{DynamicResource PlayListItem}" ScrollViewer.VerticalScrollBarVisibility="Hidden">
            <ListBox.Resources>
                <DataTemplate x:Key="PlayListItem">
                    <Grid d:DesignWidth="127" d:DesignHeight="105" Width="128" Height="128">
                        <Label x:Name="L_PlayListName" Content="{Binding Path=PlayList_Name}" HorizontalAlignment="Stretch" Margin="16" Width="Auto" Height="Auto" Background="#26000000" Padding="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontFamily="BankGothic Lt BT"/>
                    </Grid>
                </DataTemplate>
            </ListBox.Resources>
  </ListBox>

How can I add a new Item using code (dynamic) changing L_PlayListName content everytime with different name? 如何使用代码(动态)添加新项目每次使用不同的名称更改L_PlayListName内容? I need to make a new class or something? 我需要创建一个新的类或什么? I have a button to add new Item. 我有一个添加新项目的按钮。 How can I do this? 我怎样才能做到这一点?

First create a collection property in your code behind: 首先在代码后面创建一个集合属性:

public ObservableCollection<string> PlayListNames { get; set; }

Then Bind this to the ListBox.ItemsSource : 然后将其BindListBox.ItemsSource

<ListBox ItemsSource="{Binding PlayListNames}" ... />

Change your XAML slightly: 稍微更改您的XAML:

<Label x:Name="L_PlayListName" Content="{Binding}" />

This will Bind to the whole value provided to each item and in this case, that means one of the string values from the collection. 这将Bind到提供给每个项目的整个值,在这种情况下,这意味着集合中的一个string值。

Finally, just set your DataContext of your Window . 最后,只需设置WindowDataContext There are several ways to do this and the easiest (but not best) is to do this in your MainWindow.xaml.cs constructor: 有几种方法可以做到这一点,最简单的(但不是最好的)是在MainWindow.xaml.cs构造函数中执行此操作:

DataContext = this;

That's it... let me know if you have any problems. 就是这样......如果你有任何问题,请告诉我。

<Grid x:Name="LayoutRoot">
    <ListBox Margin="10" ItemsSource="{Binding}"/>
</Grid>

in front of Binding u can write name of table's column (table of database) 在Binding前面你可以写表的列名(数据库表)

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    BindData();          
}

private void BindData()
{
    DataSet dtSet = new DataSet();
    using (connection = new SqlConnection(connectionString))
    {
        command = new SqlCommand(sql, connection);              
        SqlDataAdapter adapter = new SqlDataAdapter();          
        connection.Open();
        adapter.SelectCommand = command;
        adapter.Fill(dtSet, "Customers");
        listBox1.DataContext = dtSet;

    }
}

O'k if your just adding strings to your playlist you should edit the template to the following 如果您只是在播放列表中添加字符串,则应该将模板编辑为以下内容

  <DataTemplate x:Key="PlayListItem">         
     <Label x:Name="L_PlayListName" Content="{Binding}" />           
  </DataTemplate>

But you should use an ObservableCollection and make it easy and correct . 但是你应该使用ObservableCollection并使它变得简单和正确。

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

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