Question

I have a combo box i.e editable. The combo box in my app acts like a editing control for all the datagrid cells i.e editing the value from the combo box should update the binding of my datagridtemplatecolumn. The below code updates the source if its a normal binding. If its a multibinding, it calls the convertback() function. I am using the below converter in order to update my source. The ParentID property is set to one way. I need to update only the ID property. Please help me with the convert back function

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"/>

Code

    // 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();
            }
        }


    }

Converter

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
}
Was it helpful?

Solution

Create a Converter inherited from IMultiValueConverter.

Get the TextBlock.Texts value from the Convert method instead of the StringFormat and implement the ConvertBack to set the sources.

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
}

Note:

  1. I put a space to separate the two value. This is for the split method in the ConvertBack.

  2. You set one of the bindings to OneWay

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top