为什么ConvertBack不叫这个MultiBinding? [英] Why is ConvertBack not called on this MultiBinding?

查看:249
本文介绍了为什么ConvertBack不叫这个MultiBinding?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组合框列出联系人,使用MultiBinding势必都全名和PhoneExtension。 IMul​​tiValueConverter的转换方法被调用,但ConvertBack不是。为什么? 下拉框中正确显示列表中,但选择不保存。,当我离开标签消失。

My combobox listing Contacts is bound to both FullName and PhoneExtension using MultiBinding. The Convert method of IMultiValueConverter is called but ConvertBack is not. Why? The combobox properly displays the list but the selection is not saved. It disappears when I tab away.

背景:

1)联系人列表来自于Web服务,并放在一个观察的集合ContactListObservable在code后面。我不使用一个ViewModel。

1) The Contact list comes from a web service and is put in an observable collection ContactListObservable in the code behind. I'm not using a ViewModel.

PhoneBookService phoneBookService = new PhoneBookService();
PhoneRecordArray pbs = GetCompletePhoneListing();
List<PhoneRecord> pbsList = pbs.PhoneArray.ToList();

ObservableCollection<Contact> observableContacts = new ObservableCollection<Contact>();

foreach(PhoneBookService.PhoneRecord rec in pbsList)
{
  Contact c = new Contact();
  c.FullName = rec.Person;
  c.PhoneExtension = rec.Phone;
  observableContacts.Add(c);
}

ContactListObservable = observableContacts;

2)组合框是位于一个用户控件一个DataGrid。这对于原因,这古怪的绑定:的ItemsSource ={结合ContactListObservable,的RelativeSource = {的RelativeSource FindAncestor,AncestorType =用户控件}}

3)IMultiValueConverter是在UserControl.Resources引用的类&lt;局部:CombineNameAndPhoneExtensionMultiConverter X:键=combinedNameAndPhoneExtensionConverter/&GT;

3) The IMultiValueConverter is a class referenced in UserControl.Resources as <local:CombineNameAndPhoneExtensionMultiConverter x:Key="combinedNameAndPhoneExtensionConverter"/>

4)传统数据未在联系人列表中的发现必须显示。这是通过结合一个TextBlock来显示值的DataGridTemplateColumn和用于编辑的组合框来完成。请参见这朱莉·勒曼MSDN文章

4) Legacy data NOT found in the Contacts list must display. This is accomplished with a DataGridTemplateColumn by combining a TextBlock to display values and a ComboBox for editing. See this Julie Lerman MSDN article.

下面是疯狂的XAML:

Here is the crazy XAML:

<DataGridTemplateColumn x:Name="DataGridContactTemplateColumn" Header="Contact Using Template">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding  StringFormat="{}{0} Ext. {1}">
                        <Binding Path="FullName"/>
                        <Binding Path="PhoneExtension"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate x:Name="ContactsCellEditingTemplate">
        <Grid FocusManager.FocusedElement="{Binding ElementName=ContactsTemplateComboBox}">
            <ComboBox x:Name="ContactsTemplateComboBox" IsSynchronizedWithCurrentItem="False" IsEditable="False" IsDropDownOpen="True" ItemsSource="{Binding ContactListObservable, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock DataContext="{Binding}">
                            <TextBlock.Text>                                                        
                                <MultiBinding  Converter="{StaticResource combinedNameAndPhoneExtensionConverter}">                             
                                    <Binding Path="FullName" UpdateSourceTrigger="PropertyChanged"/>                              
                                    <Binding Path="PhoneExtension" UpdateSourceTrigger="PropertyChanged"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </Grid>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

我已经投入了太多时间来这,所以我会大大AP preciate任何帮助,您可以提供。

I've devoted WAY too much time to this so I'll greatly appreciate any help you can offer.

更多的背景:

包含我的组合框的数据网格包含每行一个实体框架的接触对象,包括其他联系人字段。下面是一个成功地显示并保存全名,但有些工作不是XAML的电话分机,我想结合,以节省用真实姓名:

The datagrid containing my combobox contains one entity framework contact object per row and includes additional contact fields. Here is some working XAML that succesfully displays and saves FullName but not the phone extension which I want to save in combination with the FullName:

<DataGridTemplateColumn x:Name="DataGridContactTemplateColumn" Header="Contact Using Template">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Path=FullName}"/>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate x:Name="ContactsCellEditingTemplate">
        <Grid FocusManager.FocusedElement="{Binding ElementName=ContactsTemplateComboBox}">
            <ComboBox x:Name="ContactsTemplateComboBox" ItemsSource="{Binding ContactListObservable, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" DisplayMemberPath="FullName" SelectedValuePath="FullName" Text="{Binding Path=FullName}" SelectedItem="{Binding Path=FullName}" IsSynchronizedWithCurrentItem="False" IsEditable="False" IsDropDownOpen="True"/>
        </Grid>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

推荐答案

的TextBlock永远不会改变它的Text属性,所以没有理由调用ConvertBack方法。你将需要绑定到任何组合框的的SelectedItem或Text属性得到更新。

The TextBlock will never change it's Text property, so there is no reason to call the ConvertBack method. You would need to be bind to either the ComboBox's SelectedItem or Text properties to get updates.

这篇关于为什么ConvertBack不叫这个MultiBinding?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆