选择列表框项目时,在另一个列表框中显示相关数据

问题描述 投票:1回答:2

我正在使用WPF C#构建一个应用程序,并尝试在列表框中显示值,该列表框显示何时选择了另一个列表框中的项目。两个列表框都需要从我的SQL数据库中获取数据。

未选择任何内容时的列表框:

How listboxes should look when nothing is selected

选择项目时的kistboxes:

How listboxes should look when an item is selected

这是我在第一个列表框中显示值的方式(在任何人说出之前,我知道它很容易受到SQL注入)。

C#:

public void TradeList() {
    DataTable dt = new DataTable();
    SqlDataAdapter adpt = new SqlDataAdapter("SELECT DISTINCT Trade from tblTrades", sqlConTwo);
    adpt.Fill(dt);

    foreach(DataRow dr in dt.Rows) {
        Area.Items.Add(dr["Trade"].ToString());
    }
}

XAML:

<StackPanel>
  <TextBox x:Name="TradesSelected" Width="665" BorderBrush="#FF939393" Padding="2" BorderThickness="1"></TextBox>
  <ListBox SelectionMode="Multiple" x:Name="Trade" Width="665" Height="100" BorderBrush="#FF939393" Padding="2" BorderThickness="1 0 1 1" ItemsSource="{Binding Path=Trade}" SelectionChanged="Trade_SelectionChanged" >
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</StackPanel>
<StackPanel Margin="10 0 0 0">
  <TextBox x:Name="SkillSelected" Width="665" BorderBrush="#FF939393" Padding="2" BorderThickness="1"></TextBox>
  <ListBox SelectionMode="Multiple" x:Name="Skills" Width="665" Height="100" BorderBrush="#FF939393" Padding="2" BorderThickness="1 0 1 1" ItemsSource="{Binding Path=Skills}">
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
      </Style>
    </ListBox.ItemContainerStyle>
  </ListBox>
</StackPanel>

在我的表格中,具有相关技能的专栏称为技能。我已经找到了通过硬编码所有行业和技能来实现这一目标的方法,但这并不理想,因为我的表中有很多行,这将非常耗时。

c# sql-server wpf windows listboxitem
2个回答
2
投票

我可以看到你在ObservableCollection Skills有2个Trade ListBoxsSelectedItem将获得列表框的选择项目所以当Trade_SelectionChanged发生时你可以获得所选技能并填充Trade


1
投票

解决了Trade_SelectionChanged的问题,修复工具一直在我眼前。

© www.soinside.com 2019 - 2024. All rights reserved.