我有一个已由MySql表填充的listBox。我想改变listBox的内容无论如何用户按下按钮,我为此做的是用我的代码中的sql类创建者调用从表中过滤数据的新查询,问题是如何在该按钮事件中更改列表框内容处理程序? 这是我的代码
private void shirtSelect_Click(object sender, RoutedEventArgs e)
{
string shirt = "SELECT * FROM viewermenu.grament where type = 'shirt'";
var shirtTable = new DatabaseTable();
string id = null;
shirtTable.GetTable(shirt, id);
listBox.DataContext = shirtTable;
}
和xaml方:
<ListBox x:Name="listBox" BorderBrush="Transparent" Background="Transparent" SelectionChanged="listBox_SelectionChanged" SelectionMode="Single" ItemsSource="{Binding Source={StaticResource NamesTable}}" it HorizontalContentAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" RenderTransformOrigin="0.5,0.5" Margin="0" Padding="0,0,0,317" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="200" Width="200" >
<Image Margin="3" Source="{Binding pic_path}" RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased"/>
<TextBox Margin="3" Text="{Binding name}" Visibility="Visible"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
和databbase表类:
public DataTable GetTable(String query, String sortBy)
{
String connString = "server=192.168.*.**;uid=*****;pwd=****;database=viewermenu";
connection = new MySqlConnection(connString);
adapter = new MySqlDataAdapter(query, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataTable.DefaultView.Sort = sortBy;
return dataTable;
}
}
答案 0 :(得分:0)
给定代码中的一些错误,纠正它们并试一试。
1.将IsItemsHost
添加到ItemsPanelTemplate
:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
2.定义ShirtData
课程:
public class ShirtData
{
public string pic_path { get; set; }
public string name { get; set; }
public ShirtData(string path, string theName)
{
pic_path = path;
name = theName;
}
}
3.在代码隐藏(某些伪代码)中设置ItemsSource
:
.......
var table = shirtTable.GetTable(shirt, id);
var shirts = new List<ShirtData>();
foreach (DataRow row in table.Rows)
{
var shirtData = new ShirtData(row["pic_path"].ToString(), row["name"].ToString());
shirts.Add(shirtData);
}
listBox.ItemsSource = shirts;
答案 1 :(得分:0)
您需要修改下面ItemsSource="{Binding Source={StaticResource NamesTable}}"
绑定的属性。
list
即,假设该属性为<?php
function buildTree(array &$elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parentid'] == $parentId) {
// recursion:
$children = buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[$element['id']] = $element;
unset($elements[$element['id']]);
}
}
return $branch;
}
$rows = [
['id' => 1, 'firstname' => 'john1', 'lastname' => 'doe', 'parentid' => 0],
['id' => 2, 'firstname' => 'john2', 'lastname' => 'doe', 'parentid' => 1],
['id' => 3, 'firstname' => 'john3', 'lastname' => 'doe', 'parentid' => 1],
['id' => 4, 'firstname' => 'john4', 'lastname' => 'doe', 'parentid' => 0],
['id' => 5, 'firstname' => 'john5', 'lastname' => 'doe', 'parentid' => 3],
];
print_r(buildTree($rows));
,就像通常使用列表一样添加和删除项目。