WPF可编辑列表视图,带有可编辑的ComboBox

时间:2016-03-02 13:31:48

标签: wpf xaml listview gridview

我正在尝试使用可编辑的单元格创建一个ListView,比如当我双击一个单元格它变成一个可编辑的ComboBox时,我可以输入一些内容或从comboItems中选择。在我双击要编辑的单元格之前,我不希望看到组合框箭头(在我处于编辑模式之前,单元格不应该作为组合框出现,而是作为Label / TextBlock)。

到目前为止,我所做的是:

<GridViewColumn.CellTemplate>
    <DataTemplate>
            <ComboBox IsEditable="True" SelectedIndex="0" >
                <ComboBoxItem>One</ComboBoxItem>
                <ComboBoxItem>Two</ComboBoxItem>
                <ComboBoxItem>Three</ComboBoxItem>
            </ComboBox>
    </DataTemplate>
</GridViewColumn.CellTemplate>

但是在进入编辑模式之前我仍然可以看到ComboBox箭头(我根本就不知道如何隐藏箭头)ListView的选定行和ComboBox之间的颜色也有差异,请参阅:

enter image description here

请帮助您提供示例代码,指示或想法。提前谢谢。

PS:我是WPF的初学者

2 个答案:

答案 0 :(得分:2)

您可以将DataGrid(而不是ListView-GridView)与DataGridTemplateColumn一起使用,并为显示和编辑模式指定不同的模板(使用您的静态示例):

<DataGridTemplateColumn>
    <!-- display mode template -->
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="One" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>

    <!-- edit mode template -->
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox IsEditable="True" SelectedIndex="0" >
                <ComboBoxItem>One</ComboBoxItem>
                <ComboBoxItem>Two</ComboBoxItem>
                <ComboBoxItem>Three</ComboBoxItem>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

<小时/> 实际上,您希望绑定数据,以便您的ComboBox实际选择一些内容,TextBox显示该选择:

 <DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding SelectedNumber}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox IsEditable="True" SelectedIndex="0" 
                      ItemsSource="{Binding Numbers}" SelectedItem="{Binding SelectedNumber}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

答案 1 :(得分:1)

使用正常和其他两个不同的模板进行编辑。并显示/隐藏。

你可以进一步发展这种方法。

        <ListView.Resources>
            <DataTemplate x:Key="Tmpl1">
                <TextBlock Text="{Binding Name}" GotFocus="TextBlock_GotFocus" MouseDown="TextBlock_MouseDown_1"/>
            </DataTemplate>
            <DataTemplate x:Key="Tmpl2">
                <ComboBox LostFocus="ComboBox_LostFocus_1"/>
            </DataTemplate>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" CellTemplate="{StaticResource Tmpl1}"/>
            </GridView>
        </ListView.View>

代码:

private void TextBlock_MouseDown_1(object sender, MouseButtonEventArgs e)
{
    ((GridView)ListView1.View).Columns[0].CellTemplate = (DataTemplate)ListView1.FindResource("Tmpl2");
}

private void ComboBox_LostFocus_1(object sender, RoutedEventArgs e)
{
    ((GridView)ListView1.View).Columns[0].CellTemplate = (DataTemplate)ListView1.FindResource("Tmpl1");
}
相关问题