【问题标题】:silverlight Textbox not updated when underlying collection's corresponding property is changed更改基础集合的相应属性时,silverlight 文本框未更新
【发布时间】:2011-08-25 06:00:51
【问题描述】:

举个例子

我有 2 个文本框。两个文本框的文本都绑定到类 Name {String fullname, String funnyName};它们不是直接绑定的,而是由转换器绑定的

他们正在实现INotifyChanged 和绑定DataContextObservableCollection 和所有其他标准的东西。

这个模板是绑定的,所以我一行有 2 个文本框,列表框有 10 行 问题是:

当我在文本框 1 中更改全名时,我会去更改绑定集合中的 funname。

这不会立即反映在 GUI 上。

我怎样才能做到这一点?我不想更新整个列表框,也不想直接将它绑定到我的类中的另一个属性,而是通过转换器。当属性从“TOm”变为“dick”时,不会调用转换器,即仅第一次调用转换器。接下来每当某些属性发生变化和

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

被调用,转换器未被调用。

已添加原代码

集合类

public class VariableData : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

字符串_来源;

    /// <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
}

【问题讨论】:

    标签: c# silverlight binding observablecollection converter


    【解决方案1】:

    您似乎将整个VariableData 绑定为值,然后使用转换器提取您需要的输出。由于VariableData 实例本身没有改变(您的ConvertBack 没有返回VariableData 类型的新对象),因此用户界面没有理由相信它需要更新其用户界面。

    您应该做的是删除转换器并绑定到VariableData 的属性,将您需要的逻辑移动到VariableData 中,根据需要创建其他属性。如果更改一个属性也会影响另一个属性,您可以确保为这两个属性引发 PropertyChanged 事件。

    【讨论】:

      【解决方案2】:

      您是否在 XAML 中设置了Mode=TwoWay

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

      (目前不在 IDE 中,因此可能存在拼写错误)

      这意味着当MyProperty 改变时UI 更新,当UI 改变时MyProperty 更新。

      【讨论】:

      • no .. 仍然没有两种方式绑定 ... 说 ter 是我的 datacontext colelction 中的 10 个对象 ... 我编辑第 1 行对象属性并单击提交 .. 单击处理程序会去更新行1 个对象属性以及第 5 行和第 10 行对象属性...我希望 gui 反映对第 5 行和第 10 行所做的更改...请注意,所有文本属性都绑定到转换器 ....
      • @pskk - 您可以将您的 xaml 编辑到您的问题中(显示您的收藏的位)。我无法想象这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 2016-03-19
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      相关资源
      最近更新 更多