0

如何在列表框顶部添加空白条目并且该条目应该是可编辑的。

我编写了以下代码,该代码添加了空白条目,但不允许我进行编辑。我应该如何使它可编辑?

private void CustomButton_Click(object sender, RoutedEventArgs e)
    {
        ListBoxItem itm = new ListBoxItem();
        itm.Content = "";

        listBox1.Items.Insert(0,itm);
    }

在我的 listBox 中,我关注了一些事情,

<DataTemplate x:Key="DefaultDataTemplate" >
            <StackPanel Orientation="Horizontal" Width="596">
                <TextBox Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
                <TextBox Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
                <TextBox Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
                <TextBox Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
                <TextBox Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
                <TextBox Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>  
            </StackPanel>
        </DataTemplate>

        <!-- Editable DataTemplate -->
        <DataTemplate x:Key="EditableDataTemplate">
                <StackPanel Orientation="Horizontal" Width="596">
                <!--<ComboBox x:Name="ClientComboBox" SelectionChanged="ClientComboBoxChanged" ItemsSource="{Binding Path=clientList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ClientNameBindingClass, Mode=OneWayToSource}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="145"/>-->
                <TextBox Text="{Binding ClientNameBinding,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
                <TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
                <TextBox Text="{Binding StartTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="100"/>
                <TextBox Text="{Binding StopTimeBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="60"/>
                <TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="130"/>
                <ComboBox x:Name="ProjectComboBox" SelectionChanged="ProjectComboBoxChanged" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name"  SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="71" Background="Yellow" BorderThickness="0"/>
            </StackPanel>
        </DataTemplate>!

这是图像:

在此处输入图像描述

4

1 回答 1

0

ListBox不可编辑。您可以创建 atextbox并将您的值放入其中,然后将该值分配给listboxitem.

另一种选择是使用DataGridView(如评论中所述)。它允许编辑。

于 2013-07-05T06:02:50.500 回答