【问题标题】:WPF Editable Listview with an editable ComboBox带有可编辑组合框的 WPF 可编辑列表视图
【发布时间】:2016-03-02 13:31:47
【问题描述】:

我正在尝试创建一个带有可编辑单元格的 ListView,比如当我双击一个单元格时,它会变成一个可编辑的 ComboBox,然后我可以输入一些内容或从组合项中进行选择。在我双击一个单元格进行编辑之前,我不希望看到组合框箭头(在我处于编辑模式之前,单元格不应该将自己显示为组合框,而是显示为标签/文本块)。

到目前为止我所做的是:

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

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

请您提供示例代码、指针或想法。提前致谢。

PS:我是 WPF 的初学者

【问题讨论】:

    标签: wpf xaml listview gridview


    【解决方案1】:

    您可以将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>
    

    【讨论】:

      【解决方案2】:

      使用两种不同的模板,一种是普通模板,另一种用于编辑。并显示/隐藏。

      您可以在此方法的基础上进一步发展。

              <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");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-04
        • 2013-11-03
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多