C# 使用转换器在列表视图中绑定Silverlight对象

C# 使用转换器在列表视图中绑定Silverlight对象,c#,silverlight,windows-phone-7,xaml,C#,Silverlight,Windows Phone 7,Xaml,您好,我在强制更新列表视图时遇到了一个小问题。 我有一个ObservableCollection,它用属性缓冲区保存TestClass对象 ObservalBe集合位于ViewModel BaseViewModel中 public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); this.Data

您好,我在强制更新列表视图时遇到了一个小问题。 我有一个ObservableCollection,它用属性缓冲区保存TestClass对象

ObservalBe集合位于ViewModel BaseViewModel中

public partial class MainPage : PhoneApplicationPage {
   // Constructor
    public MainPage() {
      InitializeComponent();
      this.DataContext = new BaseViewModel();
    }
  }
}

public class BaseViewModel : INotifyPropertyChanged {
  public ObservableCollection<TestClass> TestList { get; set; }
  public BaseViewModel() {
    TestList = new ObservableCollection<TestClass>();
    TestList.Add(new TestClass() { Buffer = "1", SomeValue = 1 });
    TestList.Add(new TestClass() { Buffer = "2", SomeValue = 2 });
    TestList.Add(new TestClass() { Buffer = "3", SomeValue = 3 });
    TestList.Add(new TestClass() { Buffer = "4", SomeValue = 4 });
  }

  private TestClass selectedItem;
  public TestClass SelectedItem {
    get {
      return selectedItem;
    }
    set {
      if (selectedItem == value) {
        return;
      }

      selectedItem = value;
      selectedItem.Buffer += "a";
      selectedItem.SomeValue += 1;
      selectedItem = null;
      RaisePropertyChanged("SelectedItem");
    }
  }

  #region notifie property changed
  public event PropertyChangedEventHandler PropertyChanged;

  public void RaisePropertyChanged(string propertyName) {
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null) {
      handler(this, new PropertyChangedEventArgs(propertyName));
    }
  }
  #endregion
}

public class TestClass : INotifyPropertyChanged  {

  public TestClass() {
  }

  public int SomeValue { get; set; }

  private string buffer;
  public string Buffer {
    get {
      return this.buffer;
    }
    set {
      this.buffer = value;
      RaisePropertyChanged("Buffer");        
    }
  }

  #region notifie property changed
  public event PropertyChangedEventHandler PropertyChanged;

  public void RaisePropertyChanged(string propertyName) {
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null) {
      handler(this, new PropertyChangedEventArgs(propertyName));
    }
  }
  #endregion
}

如果在更新时将
TestList
更改为调用
RaisePropertyChanged
,是否有效


(如果您提供了完整的复制,我会自己检查。)

如果您在更新时将
TestList
更改为调用
RaisePropertyChanged
,是否有效


(如果您提供了完整的复制,我会自己检查。)

一旦绑定的某个组件被更新,并且如果此更新是由事件(如PropertyChanged)宣布的,则会立即更新绑定

装订

<TextBlock Text="{Binding Converter={StaticResource testConverter}}"/>

总是常量:它总是绑定到同一个列表条目。因此,无论您在这个列表条目中更改了什么,都不会通知绑定

如果引发INotifyCollectionChanged的CollectionChanged事件,则会重新评估绑定。您可以通过在已引发的PropertyChanged事件之外引发CollectionChanged事件来实现这一点,但是这会重新评估整个列表,并可能导致性能问题


编辑:您需要引发TestList的CollectionChanged事件,这可能是不可能的。您可能需要从ObservaleCollection中删除BaseViewModel才能执行此操作。

一旦绑定的一个组件被更新,并且如果此更新是由事件(如PropertyChanged)宣布的,则会立即更新绑定

装订

<TextBlock Text="{Binding Converter={StaticResource testConverter}}"/>

总是常量:它总是绑定到同一个列表条目。因此,无论您在这个列表条目中更改了什么,都不会通知绑定

如果引发INotifyCollectionChanged的CollectionChanged事件,则会重新评估绑定。您可以通过在已引发的PropertyChanged事件之外引发CollectionChanged事件来实现这一点,但是这会重新评估整个列表,并可能导致性能问题


编辑:您需要引发TestList的CollectionChanged事件,这可能是不可能的。您可能需要将BaseViewModel从ObservableCollection中删除才能执行此操作。

如何更改“TestList”,使其调用“RaisPropertyChanged”。我应该创建自己的ObservableList实现吗?。我不更改列表中的对象计数。我只更改该列表中对象的某些属性。但在此之后,我设置'SelectedIndex=null';如何更改“TestList”,使其调用“RaisPropertyChanged”。我应该创建自己的ObservableList实现吗?。我不更改列表中的对象计数。我只更改该列表中对象的某些属性。但在此之后,我设置'SelectedIndex=null';也许我应该用不同的方式来做。有什么建议吗?这种情况有什么模式吗?取决于你的最终目标…-想法0(通常无用):如果转换器不需要该值,则将其绑定到缓冲区。这将触发重新评估。-想法1(DirtyHack,fast):向TestClass添加一个属性,该属性返回类实例本身,将其绑定到转换器,并为缓冲区和此新属性引发PropertyChanged。-想法2(干净、通用、缓慢):为TestClass及其所有派生类创建模板,实现ItemTemplateSelector/DataTemplateSelector,就像我应该以不同的方式完成它一样。有什么建议吗?这种情况有什么模式吗?取决于你的最终目标…-想法0(通常无用):如果转换器不需要该值,则将其绑定到缓冲区。这将触发重新评估。-想法1(DirtyHack,fast):向TestClass添加一个属性,该属性返回类实例本身,将其绑定到转换器,并为缓冲区和此新属性引发PropertyChanged。-想法2(干净、通用、缓慢):为TestClass及其所有派生类创建模板,实现ItemTemplateSelector/DataTemplateSelector,如
<TextBlock Text="{Binding Converter={StaticResource testConverter}}"/>