2016-03-02 210 views
0

我想創建一個帶有可編輯單元格的ListView,比如說當我雙擊一個單元格時它變成了一個可編輯的ComboBox,然後我可以輸入一些東西或從comboItems中選擇。在我雙擊一個單元格進行編輯之前,我不希望看到組合框箭頭(一個單元格不應該在編輯模式之前呈現爲組合框,而是作爲Label/TextBlock)。WPF可編輯列表視圖與可編輯組合框

我迄今所做的是這樣的:

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

但我仍然可以看到ComboBox箭頭在編輯模式下得到(我只是不知道如何隱藏箭頭)也存在差異之前ListView的選擇行和組合框之間的顏色,請參閱:

enter image description here

請您與示例代碼,指針或想法幫助。提前致謝。

PS:我在WPF初學者

回答

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

使用兩個不同的模板一個正常和其他編輯。並顯示/隱藏。

您可以在此基礎上進一步發展。

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