【发布时间】:2016-04-12 14:44:52
【问题描述】:
我有一个 wpf ListBox,我将 DataSet 转换为 string[] 数组,我试图在列表框中显示该数组,但它没有显示。
这是我的代码!!!
string query = " SELECT category_id, category_name, amount FROM acc_income_category WHERE deleted = 0 ORDER BY category_name ASC ";
da = new SQLiteDataAdapter(query, con);
ds = new DataSet();
da.Fill(ds, "acc_income_category");
string[] arr = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
arr[i] = ds.Tables[0].Rows[i]["category_name"].ToString();
}
ListBox_Category_Names.ItemsSource = arr;
这是我的 WPF ListBox XAML 代码
<ListBox x:Name="ListBox_Category_Names" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
Width="auto" Height="auto" SelectionMode="Multiple" ItemsSource="{Binding acc_income_category}" Grid.Row="1" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<CheckBox x:Name="chkBox_CheckCategory" Grid.Column="0" Width="35" Height="35" Cursor="Hand" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}" />
<!--<TextBlock Text="{Binding category_name}" Grid.Column="1" FontSize="15" Foreground="#FF666666" Margin="10,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBlock Text="{Binding amount, ConverterCulture=ig-NG, StringFormat=\{0:C\}}" Grid.Column="3" FontSize="15" Foreground="#FF666666" Margin="5,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>-->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
【问题讨论】:
-
为什么是字符串数组?使用绑定,见:stackoverflow.com/questions/12363369/…
-
如果你只使用
.ToList()绑定回 ListBox 会发生什么ListBox_Category_Names.ItemsSource = arr.ToList();尝试在下面搜索一些 stackoverflow 示例C# stackoverflow Binding Listbox to List<object>有很多方法可以做到这一点 -
谢谢,但我正在尝试将数据集转换为数组,以将整个列记录存储在数组中。并将记录显示为数组。我该怎么做呢。
-
我使用数组的主要原因是因为我有一个按钮,可以将列表框中的选定项目传输到另一个 lisbox。或者我有什么其他方法可以解决这个问题..
标签: c# arrays wpf xaml listbox