2017-10-15 38 views
0

我有一个已经由MySql表填充的列表框。我想改变listBox的内容,无论如何用户按下按钮,我为此做的是在我的代码中调用sql类创建器,使用从表中过滤数据的新查询,问题是如何在该按钮事件中更改列表框内容处理程序? 这里是我的代码通过点击更改MySQL的列表框内容

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

在给定的代码一些错误,予以更正,有一个尝试。

1.增加IsItemsHostItemsPanelTemplate

<ListBox.ItemsPanel> 
    <ItemsPanelTemplate> 
     <StackPanel Orientation="Horizontal" IsItemsHost="True"/> 
    </ItemsPanelTemplate> 
</ListBox.ItemsPanel> 

2.Define一个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; 
+0

TNX @Iron,我这样做,但有此错误:严重\t代码\t说明\t项目\t文件\t线\t抑制状态 错误\t CS0266 \t无法隐式转换类型“Viewer.DatabaseTable”到“System.Collections.IEnumerable ”。存在明确的转换(您是否缺少演员?) – samane

+0

您能否告诉我DatabaseTable类,它可能是IEnumerable。 – Iron

+0

我已根据下面的附加问题改进答案。它会工作,但正如@KyloRen所说,更好的方法应该是'ObservableCollection',只定义和绑定一次。 – Iron

0

您需要修改属性tha下面的t绑定为NamesTable

ItemsSource="{Binding Source={StaticResource NamesTable}}" 

即,假设属性是list,添加和删除的项目,你会通常与清单做。

+0

tnx @KyloRen,NamesTable是一个DatabaseTable,我应该把它转换为列表? – samane

+0

@samane,你应该用'ItemsSource'绑定到一个列表。做一个列表属性,即'列表 yourList =新列表();'并且绑定到那个。理想情况下,您应该使用MVVM并绑定到一个'ObservableCollection'。 – KyloRen

相关问题