【问题标题】:Updating the source with multibinding使用多重绑定更新源
【发布时间】:2012-09-21 20:09:07
【问题描述】:

我有一个组合框,即可编辑。我的应用程序中的组合框就像所有数据网格单元格的编辑控件,即编辑组合框中的值应该更新我的数据网格模板列的绑定。如果它是一个正常的绑定,下面的代码会更新源代码。如果它是多重绑定,它会调用 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
}

【问题讨论】:

    标签: c# wpf data-binding wpfdatagrid multibinding


    【解决方案1】:

    创建一个继承自IMultiValueConverter的转换器。

    Convert 方法而不是 StringFormat 获取 TextBlock.Texts 值并实现 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 中的 split 方法。

    2. 您将其中一个绑定设置为OneWay

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 2014-09-08
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 2016-12-08
      • 1970-01-01
      相关资源
      最近更新 更多