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
Skills
和Trade
, SelectedItem
将获得列表框的选择项,所以当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.