2012-05-08 30 views
1

我有一個MultiBinding無法在TextBox.Text上工作。我具有與Extended WPF Toolkit的IntegerUpDown值正確綁定的相同代碼。Multibinding無法在TextBox.Text上工作

正是通過採用結合的POCO並且它是(它在列表框顯示的項的順序)

這裏的一部分的列表框的IMultiValueConverter打算是代碼:

<!--works--> 
<wpf:IntegerUpDown ValueChanged="orderChanged" x:Name="tripOrder"> 
    <wpf:IntegerUpDown.Value> 
     <MultiBinding Converter="{StaticResource listBoxIndexConverter}" Mode="OneWay"> 
      <Binding /> 
      <Binding ElementName="listTrips" /> 
     </MultiBinding> 
    </wpf:IntegerUpDown.Value> 
</wpf:IntegerUpDown> 
<!--doesn't work--> 
<TextBox x:Name="tripOrder2"> 
    <TextBox.Text> 
     <MultiBinding Converter="{StaticResource listBoxIndexConverter}" Mode="OneWay"> 
      <Binding /> 
      <Binding ElementName="listTrips" /> 
     </MultiBinding> 
    </TextBox.Text> 
</TextBox> 

下面是結果:

result

我不認爲這是相關的,但爲了以防萬一,這裏是PE類rforms轉換:

public class ListBoxIndexConverter : IMultiValueConverter 
    { 

    #region IMultiValueConverter Members 

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var trip = values[0] as TripBase; 

     if (trip == null) 
     { 
      return null; 
     } 

     var lb = values[1] as CheckListBox; 
     if (lb == null) 
     { 
      return null; 
     } 

     //make it 1 based 
     return lb.Items.IndexOf(trip) + 1; 
    } 

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

    #endregion 
} 
+0

何時綁定屬性觸發其PropertyChanged事件。你能否證實他們在你期望綁定被更新的時候發射? – Alain

回答

2

轉換器應該返回屬性期望的類型。原因在於,在經常使用這些屬性(即沒有0​​)時,這些屬性可能具有從一種類型(或多種)轉換爲屬性所需類型的類型轉換器。例如,當你寫:

<ColumnDefinition Width="Auto"/> 

有這麼轉換字符串「自動」轉換器:

new GridLength(1, GridUnitType.Auto) 

當使用綁定,這種機制被忽略,因爲轉換器應該返回正確的類型。

因此,要解決您的問題,你的轉換器返回:

return (lb.Items.IndexOf(trip) + 1).ToString(); 

這應該可以解決TextBox

現在,爲IntegerUpDown。這聽起來像它實際上期望收到一個int和返回一個字符串將打破它。因此,再次改變轉換器的返回值:

if (targetType == typeof(int)) 
{ 
    return lb.Items.IndexOf(trip) + 1; 
} 
else if (targetType == typeof(string)) 
{ 
    return (lb.Items.IndexOf(trip) + 1).ToString(); 
} 
else 
{ 
    throw new NotImplementedException(String.Format("Can not convert to type {0}", targetType.ToString())); 
} 
0

綁定是行不通的,因爲listTrips沒有改變時,列表框的選擇值的變化。改變的事情是listTrips.SelectedItem,所以你應該綁定反對:

<Binding Path="SelectedItem" ElementName="listTrips"/> 

其實,我不知道爲什麼它工作的第一個例子。

+0

'listTrips'是一個列表框 –

+0

@Tim:用'SelectedItem'替換'Text'。 – Vlad

+0

''是實現'INotifyPropertyChanged'的POCO(TripBase) –