2011-05-13 82 views
0

作爲示例silverlight Textbox在底層集合的相應屬性發生更改時未更新

我有2個文本框。兩個文本框的文本都綁定到類名{String fullname,String funnyName};他們不是直接綁定,而是通過轉換器

他們正在實施INotifyChanged和綁定DataContextObservableCollection和所有其他標準的東西。

此模板綁定,因此我有一個2個文本框和列表框有10行 的問題是:

,當我在文本框中輸入1更改全名,我去更改的funnyname綁定的集合。

這不會立即反映到GUI上。

我該如何做到這一點?我不想更新整個列表框,我不想直接將其綁定到我的課程中的另一個屬性,而是通過轉換器。當屬性從「TOm」改變爲「dick」時,轉換器不會被調用,即只有第一次調用轉換器。接下來每當有些財產發生變化時,

this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("FunnyName")); 

被調用,轉換器不被調用。

增加了原代碼

集合類

public class VariableData : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

字符串_Source;

/// <summary> 
    /// Gets or sets the source. 
    /// </summary> 
    /// <value> 
    /// The source. 
    /// </value> 
    public String Source 
    { 
     get { return _Source; } 
     set 
     { 
      _Source = value; if (this.PropertyChanged != null) 
       this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Source")); 
     } 
    } 
} 

結合

<TextBox Name="textBoxFileLocation" 
     Text="{Binding Converter={StaticResource mapTypeToDataConverter}, ConverterParameter=41}" 
     Margin="5,5,5,5" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"> 
</TextBox> 

轉換器

public class MapTypeToDataConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    /// <summary> 
    /// Modifies the source data before passing it to the target for display in the UI. 
    /// </summary> 
    /// <param name="value">The source data being passed to the target.</param> 
    /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param> 
    /// <param name="parameter">An optional parameter to be used in the converter logic.</param> 
    /// <param name="culture">The culture of the conversion.</param> 
    /// <returns> 
    /// The value to be passed to the target dependency property. 
    /// </returns> 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
      return ""; 

     VariableData cus = null; 
     try 
     { 
      cus = (VariableData)value; 
     } 
     catch (Exception e3) 
     { 
      return null; 
     } 
     if (cus == null) 
      return ""; 

     int temp = int.Parse(parameter.ToString()); 

     int mapType = temp/10; 
     int whatToreturn = temp % 10; 

     if (mapType != cus.MappingType) 
     { 
      if (whatToreturn == 3) 
       return false; 
      else 
       return ""; 
     } 

     switch (whatToreturn) 
     { 
      case 1: 
       return cus.Source; 
       break; 
      case 2: 
       return cus.Query; 
       break; 
      case 3: 
       if (cus.Source != null && cus.Source.Length > 0) 
        return true; 
       else 
        return false; 
     } 

     return ""; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return (int)value; 
    } 

    #endregion IValueConverter Members 
} 

回答

1

您似乎將整個VariableData綁定爲值,然後使用轉換器提取所需的輸出。由於VariableData實例本身並未發生變化(您的ConvertBack未返回類型爲VariableData的新對象),因此UI沒有理由認爲它需要更新其UI。

你應該做的是放置轉換器並綁定到VariableData的屬性,將需要的邏輯移動到VariableData根據需要創建其他屬性。如果更改一個屬性也會影響另一個屬性,則可以確保爲兩個屬性都引發PropertyChanged事件。

1

你有沒有設置Mode=TwoWay在XAML:

<TextBox Text="{Binding MyProperty, Mode=TwoWay, Converter={StaticResource myConverter}}" /> 

(而不是在目前的IDE所以可能會有錯別字)

這將意味着UI更新時MyProperty變化以及MyProperty更新時,用戶界面的變化。

+0

no ..仍然沒有兩個waybinding ...說ter是我的datacontext colelction中的10個對象...我編輯第1行對象屬性,然後單擊提交..點擊處理程序去更新行1對象屬性,也第5行和第10行對象屬性...我希望gui反映對第5行和第10行進行的更改...並且請注意,所有文本屬性都綁定到轉換器.... – pskk 2011-05-13 21:03:48

+0

@pskk - 你能編輯你的xaml進入你的問題(顯示你的收藏的位)。我無法想象這個問題。 – ChrisF 2011-05-13 21:05:57

+0

..用代碼更新了問題......謝謝 – pskk 2011-05-13 21:14:22

相關問題