向ListBox添加空白条目,该条目应该是可编辑的

时间:2013-07-05 05:58:39

标签: c# wpf insert listboxitem

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

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

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>!

这是图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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