简体   繁体   中英

When listbox item is selected display related data in another listbox

I am building an app with WPF C# and trying to display values in a list box which shows when an item from another list box is selected. Both list boxes need to get data from my SQL database.

listboxes when nothing is selected:

当没有选择任何内容时,列表框应该如何显示

kistboxes when an item is selected:

选择项目时,列表框应如何显示

This is how I display the values in the first list box (before anyone says it, I know it is vulnerable to SQL injection).

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>

In my table, the column with the skills in it is called Skills. I have found a way to do it by hard-coding all the trades and skills in, but this is not ideal as there are a lot of rows in my table, and this would be very time-consuming.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM