【问题标题】:WPF ListBox - how to displays values from datatable with multiple columns?WPF ListBox - 如何显示具有多列的数据表中的值?
【发布时间】:2012-06-19 14:01:03
【问题描述】:

我遇到了问题,我有一个包含 3 列(ID、NAME、QUANTITY)的数据表 data,我想将它与 ListBox 绑定,并使 ListBox 显示列 NAME 和QUANTITY,否则当我双击选定的项目时,它会发送 ID 值,这是我的 XAML:

<ListBox  HorizontalAlignment="Left" Margin="6,0,0,0" Name="ListBox1" VerticalAlignment="Top" Height="600" Width="321">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ListBoxItem}">
                                <EventSetter Event="MouseDoubleClick" Handler="ListBox1Item_DoubleClick" />
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.Resources>
                            <DataTemplate x:Key="listBoxTemplate">
                                <DockPanel >
                                    <TextBlock FontWeight="Bold" Text="{Binding NAME}"
                  DockPanel.Dock="Left"
                  Margin="5,0,10,0" Width="100"/>

                                    <TextBlock Text="{Binding QUANTITY} "   Foreground="Green" FontWeight="Bold" />
                                </DockPanel>
                            </DataTemplate>
                        </ListBox.Resources>
                    </ListBox> 

这是我的代码:

...
            ListBox1.ItemsSource = data.DefaultView;
            ListBox1.SelectedValuePath = "ID";
...

但它没有显示任何东西,有什么问题吗?请帮忙!感谢您阅读本文!

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

    您需要设置 ListBox.ItemTemplate。 目前,您正在使用密钥定义模板,该模板并未在任何地方使用。

    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel >
                <TextBlock Text="{Binding NAME}" FontWeight="Bold"
                           DockPanel.Dock="Left"
                           Margin="5,0,10,0" Width="100" />
                <TextBlock Text="{Binding QUANTITY}" FontWeight="Bold"
                           Foreground="Green" />
            </DockPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2010-12-19
      • 2018-04-22
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多