2011-04-04 54 views
1

我有一个具有两个ObservableCollections的多重绑定的DataBinding,并且我想在使用MultiConverter的条件之间切换它们。 所以转换器给出了正确的集合,但绑定似乎没有更新。使用多重绑定切换绑定源

任何想法?

映入眼帘,

于尔根

回答

4

这是你需要转换器:

public class SwitchCollectionsConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool b = (bool)values[2]; 

     if (b) 
      return values[0]; 
     else 
      return values[1]; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

注册的转换器:绑定的

<local:SwitchCollectionsConverter x:Key="TheConverter" /> 

用法:

<ItemsControl> 
     <ItemsControl.ItemsSource> 
      <MultiBinding Converter="{StaticResource TheConverter}"> 
       <Binding Path="FirstCollection" /> 
       <Binding Path="SecondCollection" /> 
       <Binding Path="IsFirst" /> 
      </MultiBinding> 
     </ItemsControl.ItemsSource> 
    </ItemsControl> 

,你有一个FirstCollection,一个SecondCollection和IsFirst属性中的DataContext

+0

那是我有什么,它似乎是工作,不过我的看法似乎并没有被更新,如果原来的结合是其替换另一个来自MultiBinding。我总是有树中显示的旧数据... – opiswahn 2011-04-04 08:51:13

+2

您是否在所有三个属性上实现了INotifyPropertyChanged? (是的,在这里ObservableCollections不会帮助) – 2011-04-04 08:54:26

+0

我认为这与它无关。可以说原始绑定在FirstCollection上,并且将改变为SecondCollection,所以Binging会改变,而不是集合本身......或者我在这里错了吗? – opiswahn 2011-04-04 10:59:50

0

的假设下,你需要更新源列表视图?

如果是这样,你的结合应该是双向模式:

<TextBox Text="{Binding Source, Mode="TwoWay"}" />