如何在WPF中刷新绑定?

Joa*_*nge 6 .net c# wpf

我有一些ObservableCollections绑定到一些WPF控件,它们工作正常.我有一个功能,我通过重新分配完全替换这些ObservableCollections并再次填充它们,但在这之后,WPF控件不会更新.

或者这个绑定连接只在启动时建立一次,然后我永远不应该重新初始化ObservableCollections,但只改变它们?

编辑:

public partial class MainWindow : Window
{
    ObservableCollection<EffectViewModel> effects;
    public ObservableCollection<EffectViewModel> Effects
    {
        get { return this.effects; }
        set
        {
            this.effects = value;
            this.RaisePropertyChanged ( "Effects" );
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged ( string name )
    {
        var handler = this.PropertyChanged;
        if ( handler != null )
            handler ( this, new PropertyChangedEventArgs ( name ) );
    }
}

public void LoadEffects ( string path, string filename )
{
    //returns new ObservableCollection<EffectViewModel> ( );
    this.Effects = File.Load ( path, filename );
}

public class EffectViewModel
{
    public bool this [ EffectType type ]
    {
        get { return AllEffects.First ( e => e.Type == this.Type ).IsSupported; }
        set
        {
            AllEffects.First ( e => e.Type == this.Type ).IsSupported = value;
            this.RaisePropertyChanged ( "this" );
        }
    }

    #region Events

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged ( string name )
    {
        var handler = this.PropertyChanged;
        if ( handler != null )
            handler ( this, new PropertyChangedEventArgs ( name ) );
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

EDIT2:

<Window x:Class="EffectWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="Effect Display" Height="200" Width="700"
    <DockPanel VerticalAlignment="Stretch">

        <ListView
            ItemsSource="{Binding Effects}"
            AlternationCount="2"
            DockPanel.Dock="Top"
            HorizontalContentAlignment="Stretch">

            <ListView.View>
                <GridView>

                    <GridViewColumn
                        Width="70"
                        Header="GPU">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox
                                    Margin="0"
                                    HorizontalAlignment="Center"
                                    IsChecked="{Binding [GPU], Mode=TwoWay}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn
                        Width="70"
                        Header="CPU">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox
                                    Margin="0"
                                    HorizontalAlignment="Center"
                                    IsChecked="{Binding [CPU], Mode=TwoWay}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>

    </DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

Pav*_*kov 5

您绑定的对象应该实现该INotifyPropertyChanged接口.然后,绑定的collection属性应该PropertyChanged在其setter中引发事件.像这样的东西:

public ObservableCollection<MyObject> MyCollection
{
   get
   {
      return _myCollection;
   }
   set
   {
      _myCollection = value;
      RaisePropertyChanged("MyCollection");
   }
} 
Run Code Online (Sandbox Code Playgroud)

  • @Joan - 好的,现在我看到了问题.您的窗口不会继承`INotifyPropertyChanged`接口(以及`EffectViewModel`).但它应该:`public partial class MainWindow:Window,INotifyPropertyChanged`.至于为indexer属性引发`PropertyChanged`事件,将空字符串指定为属性名参数应该有效. (2认同)