2011-05-13 49 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

相关问题