【发布时间】:2018-05-02 08:01:44
【问题描述】:
我有列表框,我需要从代码中设置它的选定值。它没有被选中给定值。我正在处理 WPF 应用程序。请帮我写代码。 以下是我的代码:
<ListBox x:Name="lbCheque" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,191,0,0"
Width="200" Height="210" SelectionChanged="lbCheque_SelectionChanged" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
后面的代码:
lbCheque.SelectedItem = "abcd";
【问题讨论】:
-
绑定的 ItemsSource 类型是什么?是 List
吗? -
不确定您的意思是“从代码中设置其选定的值”。您的意思是要将 ListItem 从您的代码隐藏添加到您当前的 ListBox 中吗?
-
我需要从 ListItems 中设置选定的值