2012-09-30 44 views
2

我有一個組合框,即可編輯。我應用程序中的組合框就像所有數據網格單元格的編輯控件,即編輯組合框中的值應更新我的datagridtemplate列的綁定。如果下面的代碼是正常的綁定,則更新源代碼。如果它是一個多重綁定,它會調用convertback()函數。我正在使用下面的轉換器來更新我的源代碼。 ParentID屬性設置爲一種方式。我只需要更新ID屬性。請幫我重新轉換功能用多重綁定更新源

的XAML

<tk:Datagrid> 
    <tk:DataGridTemplateColumn Header="Event ID" MinWidth="100" CellTemplate="{StaticResource ClipsEventIDCellTemplate}" CellEditingTemplate="{StaticResource ClipsEventIDCellEditingTemplate}" /> 
</tk:Datagrid> 

    <DataTemplate x:Key="ClipsEventIDCellTemplate"> 
     <TextBlock> 
      <TextBlock.Text> 
       <MultiBinding UpdateSourceTrigger="Explicit" Converter="{StaticResource EventIDConvert}" Mode="TwoWay" UpdateSourceTrigger="Explicit" > 
        <Binding Path="ParentID" Mode="OneWay"/> 
        <Binding Path="ID" Mode="TwoWay"/> 
       </MultiBinding> 
      </TextBlock.Text> 
     </TextBlock> 
    </DataTemplate> 

<ComboBox x:Name="UniversalTextBox" IsEditable="True" ItemsSource="{Binding UniversalTextEntries, ElementName=TheMainWindow, Mode=OneWay}" KeyDown="OnUniversalTextKeyDown"/> 

代碼

// properties 
    public int ID 
    { 
     get { return m_id; } 
     set 
     { 
      if (m_id != value) 
      { 
       m_id = value; 
       NotifyPropertyChanged("ID"); 
      } 
     } 
    } 

    public int ParentID 
    { 
     get; 
     set; 
    } 

    private void OnUniversalTextKeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key != Key.Enter && e.Key != Key.Escape) 
      return; 

     var comboBox = sender as ComboBox; 
     if (comboBox == null) 
      return; 
     BindingExpression binding = null; 
     MultiBindingExpression multibinding = null; 

     bool restoreGridFocus = false; 
     bool isMultibinding = false; 

     binding = comboBox.GetBindingExpression(ComboBox.TextProperty); 
     if (binding == null) 
     { 
      isMultibinding = true; 
      multibinding = BindingOperations.GetMultiBindingExpression(comboBox, ComboBox.TextProperty); 
      if (multibinding == null && multibinding.BindingExpressions.Count < 0) 
       return; 
     } 

     if (e.Key == Key.Escape) 
     { 
      restoreGridFocus = true; 
      if (!isMultibinding) 
       binding.UpdateTarget(); 
      else 
       multibinding.UpdateTarget(); 
     } 
     else if (e.Key == Key.Enter) 
     { 
      PopulateTextEntries(comboBox.Text); 
      restoreGridFocus = true; 
      if (!isMultibinding) 
       binding.UpdateSource(); 
      else 
       multibinding.UpdateSource(); 
     } 

     if (restoreGridFocus)// set the focus back to the lastfocuced cell in the datagrid 
     { 
      e.Handled = true; 

      if (m_BoundDataGrid != null) 
      { 
       var cell = m_BoundDataGridCell; 

       if (cell == null) 
        cell = DataGridUtils.GetCell(m_BoundDataGrid, m_BoundObject, m_BoundColumnIndex); 

       if (cell != null) 
        cell.Focus(); 
      } 
     } 


    } 

轉換

public class EventIDConverter : IMultiValueConverter 
{ 
    #region IMultiValueConverter Members 

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (values.Length < 2) 
      return null; 


     return string.Format("{0}{1}", values[0], values[1]); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     if (value == null) 
      return null; 

     //ToDo 

     ??????????????? 
    } 

    #endregion 
} 

回答

3

創建一個從IMultiValueConverter繼承的Converter。

獲取TextBlock.Text價值從轉換方法,而不是StringFormat貫徹ConvertBack設定來源。

public class EventIDConverter : IMultiValueConverter 
{ 
#region IMultiValueConverter Members 

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
{ 
    if (values.Length < 2) 
     return null; 


    return string.Format("{0} {1}", values[0], values[1]); 
} 

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
{ 
    if (value == null) 
     return null; 

    string[] splitValues = ((string)value).Split(' '); 
    return splitValues; 
} 

#endregion 
} 

注:

  1. 我把一個空格分隔兩個值。這是在ConvertBack中的拆分方法。

  2. 您設置的綁定之一OneWay

+0

更新的代碼! – Jack