2011-11-07 16 views
7

मेरे पास डेटाग्रिड में डेटाग्रिडकंबोबॉक्सकॉल है। मैं एक बार सेल पर क्लिक करने में सक्षम होना चाहता हूं और combobox ड्रॉप डाउन है। वर्तमान में मुझे कई बार क्लिक करना है।DataGridComboBoxColumn - एक क्लिक पर ऑटो ड्रॉप डाउन

<DataGrid AutoGenerateColumns="False" Height="148" HorizontalAlignment="Left" Margin="48,85,0,0" Name ="dg_display" VerticalAlignment="Top" Width="645" CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding}" SelectionChanged="DgDisplaySelectionChanged"> 
     <DataGrid.Columns> 
      <DataGridTextColumn IsReadOnly="True" Header="Symbol" Binding="{Binding Symbol}" /> 
      <DataGridTextColumn IsReadOnly="True" Header="Company ID" Binding="{Binding CompanyID}" /> 
      <DataGridComboBoxColumn IsReadOnly="False" Header="Sector" SelectedValueBinding="{Binding Sector}" DisplayMemberPath="{Binding [0]}" Visibility="Visible" > 
       <DataGridComboBoxColumn.EditingElementStyle> 
        <Style TargetType="ComboBox"> 
         <Setter Property="ItemsSource" Value="{Binding SectorList}" /> 
        </Style> 
       </DataGridComboBoxColumn.EditingElementStyle> 
       <DataGridComboBoxColumn.ElementStyle> 
        <Style TargetType="ComboBox"> 
         <Setter Property="ItemsSource" Value="{Binding SectorList}" /> 
        </Style> 
       </DataGridComboBoxColumn.ElementStyle> 
      </DataGridComboBoxColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
+0

, आप 'DataGrid' संपादन मोड में जाने की जरूरत है अर्थात BeginningEditEvent को ऊपर उठाने? – XAMeLi

+0

मैंने शुरुआत में कभी नहीं उठाया। क्या मुझे यह करना ज़रूरी है? –

+1

सेल पर पहला क्लिक सेल पर फ़ोकस सेट कर रहा है और (शायद) इसे चुन रहा है ('डेटाग्रिड' के चयन मोड पर निर्भर करता है), दूसरा क्लिक संपादन संपादन दिखा रहा है और वह तब होता है जब BeginningEditEvent उठाया जाता है ('DataGrid द्वारा ')। तो मैं समझता हूँ कि आप इस घटना से निपटने नहीं कर रहे हैं, और न ही अपने तर्क निर्भर है कि क्या 'DataGrid' संपादन मोड में है (यानी कि क्या IsEditingCurrentCell == सच है या IsEditingRowItem == सच है), है ना? – XAMeLi

उत्तर

6

एक क्लिक DataGridComboBoxColumn संपादन + एक क्लिक CheckboxColumn संपादन
भी देखें: https://stackoverflow.com/a/8333704/724944

XAML:

 <Style TargetType="{x:Type DataGridCell}"> 
      <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" /> 
      <EventSetter Event="PreviewTextInput" Handler="DataGridCell_PreviewTextInput" /> 
     </Style> 

कोड-पीछे:

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     DataGridCell cell = sender as DataGridCell; 
     GridColumnFastEdit(cell, e); 
    } 

    private void DataGridCell_PreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     DataGridCell cell = sender as DataGridCell; 
     GridColumnFastEdit(cell, e); 
    } 

    private static void GridColumnFastEdit(DataGridCell cell, RoutedEventArgs e) 
    { 
     if (cell == null || cell.IsEditing || cell.IsReadOnly) 
      return; 

     DataGrid dataGrid = FindVisualParent<DataGrid>(cell); 
     if (dataGrid == null) 
      return; 

     if (!cell.IsFocused) 
     { 
      cell.Focus(); 
     } 

     if (cell.Content is CheckBox) 
     { 
      if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) 
      { 
       if (!cell.IsSelected) 
        cell.IsSelected = true; 
      } 
      else 
      { 
       DataGridRow row = FindVisualParent<DataGridRow>(cell); 
       if (row != null && !row.IsSelected) 
       { 
        row.IsSelected = true; 
       } 
      } 
     } 
     else 
     { 
      ComboBox cb = cell.Content as ComboBox; 
      if (cb != null) 
      { 
       //DataGrid dataGrid = FindVisualParent<DataGrid>(cell); 
       dataGrid.BeginEdit(e); 
       cell.Dispatcher.Invoke(
       DispatcherPriority.Background, 
       new Action(delegate { })); 
       cb.IsDropDownOpen = true; 
      } 
     } 
    } 


    private static T FindVisualParent<T>(UIElement element) where T : UIElement 
    { 
     UIElement parent = element; 
     while (parent != null) 
     { 
      T correctlyTyped = parent as T; 
      if (correctlyTyped != null) 
      { 
       return correctlyTyped; 
      } 

      parent = VisualTreeHelper.GetParent(parent) as UIElement; 
     } 
     return null; 
    } 
+0

"यह भी देखें" यह कैसे करना है। DataGridTemplateColumn का उपयोग करें और अपने सेल टेम्पलेट को कॉम्बोबॉक्स पर सेट करें। – user1454265

2

मैं था @ एसयू के साथ समस्याएं आरएफएन का जवाब, शायद क्योंकि यह कई सालों बाद है और डब्ल्यूपीएफ शायद थोड़ा सा बदल गया है। ऐसा लगता है कि DataGrid अब आपके लिए कुछ चीजों का ख्याल रखता है, जैसे टाइपिंग शुरू करते समय स्वचालित रूप से टेक्स्ट फ़ील्ड को संपादित करना।

मैं अपने कॉम्बो बॉक्स कॉलम के लिए DataGridTemplateColumn का उपयोग करता हूं। टेम्पलेट में के लिए TextBlock है। BeginEdit पर कॉल करने के बाद प्रेषक कॉल कॉम्बो बॉक्स को दृश्य पेड़ में प्रकट करने का कारण बनता है। ऐसा प्रतीत होता है कि माउस क्लिक कॉम्बो बॉक्स में भेजा जाता है और यह स्वयं ही खुलता है।

यहाँ @ surfen के कोड के अपने संशोधित संस्करण है:

public static class DataGridExtensions 
{ 
    public static void FastEdit(this DataGrid dataGrid) 
    { 
     dataGrid.ThrowIfNull(nameof(dataGrid)); 

     dataGrid.PreviewMouseLeftButtonDown += (sender, args) => { FastEdit(args.OriginalSource, args); }; 
    } 

    private static void FastEdit(object source, RoutedEventArgs args) 
    { 
     var dataGridCell = (source as UIElement)?.FindVisualParent<DataGridCell>(); 

     if (dataGridCell == null || dataGridCell.IsEditing || dataGridCell.IsReadOnly) 
     { 
      return; 
     } 

     var dataGrid = dataGridCell.FindVisualParent<DataGrid>(); 

     if (dataGrid == null) 
     { 
      return; 
     } 

     if (!dataGridCell.IsFocused) 
     { 
      dataGridCell.Focus(); 
     } 

     if (dataGridCell.Content is CheckBox) 
     { 
      if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) 
      { 
       if (!dataGridCell.IsSelected) 
       { 
        dataGridCell.IsSelected = true; 
       } 
      } 
      else 
      { 
       var dataGridRow = dataGridCell.FindVisualParent<DataGridRow>(); 

       if (dataGridRow != null && !dataGridRow.IsSelected) 
       { 
        dataGridRow.IsSelected = true; 
       } 
      } 
     } 
     else 
     { 
      dataGrid.BeginEdit(args); 

      dataGridCell.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { })); 
     } 
    } 
} 

public static class UIElementExtensions 
{ 
    public static T FindVisualParent<T>(this UIElement element) 
     where T : UIElement 
    { 
     UIElement currentElement = element; 

     while (currentElement != null) 
     { 
      var correctlyTyped = currentElement as T; 

      if (correctlyTyped != null) 
      { 
       return correctlyTyped; 
      } 

      currentElement = VisualTreeHelper.GetParent(currentElement) as UIElement; 
     } 

     return null; 
    } 
}