c#WPF将组合框项添加到列表框中

时间:2017-03-15 22:34:46

标签: c# wpf xaml combobox listbox

我尝试制作它,以便用户可以从组合框中选择一个项目,按一个按钮,所选项目将添加到列表框中。问题是,一旦我做出任何选择并按下按钮 - 列表框似乎会出现问题,水平滚动滑块会弹出并占据整个空间(甚至不知道项目是否被添加)。

XAML代码:

<field name="FirstName" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="Person.FirstName"/>
    </field>
    <field name="LastName" class="java.lang.String">
        <<property name="net.sf.jasperreports.json.field.expression" value="Person.LastName"/>
    </field>
    <field name="Contacts" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="Person.Contacts"/>
</field>

CS代码:

<Label Name="UserEntryLabel" Content="Your chosen entries:" Grid.Row="1" Grid.ColumnSpan="2"  Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="15" FontWeight="Bold" Foreground="DarkBlue" Margin="0,0,10,8" Padding="0,0,0,0"/>
<ListBox Name="UserEntryBox" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="120" Height="20" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="0,0,0,8"/>
<Label Name="NewEntryLabel" Content="Add new entry:" Grid.Row="2"  Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="15" FontWeight="Bold" Foreground="DarkBlue" Margin="0,8,6,0" Padding="0,0,0,0"/>
<ComboBox Name="NewEntryBox" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="120" Height="20" Margin="0,8,0,0">
    <ComboBoxItem IsSelected="True">Entry1</ComboBoxItem>
    <ComboBoxItem>Entry2</ComboBoxItem>
    <ComboBoxItem>Entry3</ComboBoxItem>
    <ComboBoxItem>Entry4</ComboBoxItem>
</ComboBox>
<Button Name="NewEntryButton" Content="Add" Grid.Row="2" Grid.Column="2" HorizontalAlignment="left" VerticalAlignment="Top" Width="90" Height="20" Margin="0,8,0,0" Click="NewChestButtonClick"/>

1 个答案:

答案 0 :(得分:1)

您的问题是您没有在组合框中添加值,但整个项目也是如此:

  

System.Windows.Controls.ComboBoxItem:Entry1

如果您更改添加组合框的值的方式,您可以看到它出现在列表框中:

private void AddNewEntry()
{
    UserEntryBox.Items.Add(NewEntryBox.Text);
}