Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用多重绑定更新源_C#_Wpf_Data Binding_Wpfdatagrid_Multibinding - Fatal编程技术网

C# 使用多重绑定更新源

C# 使用多重绑定更新源,c#,wpf,data-binding,wpfdatagrid,multibinding,C#,Wpf,Data Binding,Wpfdatagrid,Multibinding,我有一个组合框,即可编辑。我的应用程序中的组合框就像所有datagrid单元格的编辑控件,即编辑组合框中的值应更新我的datagridtemplatecolumn的绑定。如果源代码是正常绑定,下面的代码将更新源代码。如果是多重绑定,则调用convertback()函数。我正在使用下面的转换器来更新我的源代码。ParentID属性设置为单向。我只需要更新ID属性。请帮助我使用转换回功能 Xaml <tk:Datagrid> <tk:DataGridTemplateColum

我有一个组合框,即可编辑。我的应用程序中的组合框就像所有datagrid单元格的编辑控件,即编辑组合框中的值应更新我的datagridtemplatecolumn的绑定。如果源代码是正常绑定,下面的代码将更新源代码。如果是多重绑定,则调用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();
            }
        }


    }
//属性
公共整数ID
{
获取{return m_id;}
设置
{
如果(m_id!=值)
{
m_id=值;
NotifyPropertyChanged(“ID”);
}
}
}
公共int父ID
{
得到;
设置
}
UniversalTextKeyDown上的私有void(对象发送方,KeyEventArgs e)
{
如果(e.Key!=Key.Enter&&e.Key!=Key.Escape)
返回;
var comboBox=发送方作为组合框;
如果(组合框==null)
返回;
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)
返回;
}
if(e.Key==Key.Escape)
{
restoreGridFocus=true;
如果(!isMultibinding)
binding.UpdateTarget();
其他的
multibinding.UpdateTarget();
}
else if(e.Key==Key.Enter)
{
PopulateExtentries(comboBox.Text);
restoreGridFocus=true;
如果(!isMultibinding)
binding.UpdateSource();
其他的
multibinding.UpdateSource();
}
if(restoreGridFocus)//将焦点设置回数据网格中最后一个聚焦的单元格
{
e、 已处理=正确;
if(m_BoundDataGrid!=null)
{
var cell=m_BoundDataGridCell;
if(单元格==null)
cell=DataGridUtils.GetCell(m_BoundDataGrid,m_BoundObject,m_BoundColumnIndex);
如果(单元格!=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
}
public类EventIDConverter:IMultiValueConverter
{
#区域IMultiValueConverter成员
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
如果(值。长度<2)
返回null;
返回string.Format(“{0}{1}”,值[0],值[1]);
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,CultureInfo区域性)
{
如果(值==null)
返回null;
//待办事项
???????????????
}
#端区
}

创建继承自
IMultiValueConverter
的转换器

从Convert方法而不是
StringFormat
获取
TextBlock.Text
s值,并实现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
}
public类EventIDConverter:IMultiValueConverter
{
#区域IMultiValueConverter成员
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
如果(值。长度<2)
返回null;
返回string.Format(“{0}{1}”,值[0],值[1]);
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,CultureInfo区域性)
{
如果(值==null)
返回null;
字符串[]拆分值=((字符串)值)。拆分(“”);
返回值;
}
#端区
}
注意:

  • 我放了一个空格来分隔这两个值。这是用于ConvertBack中的split方法

  • 您将其中一个绑定设置为单向