开发者

How to refresh the binding in WPF?

I have some ObservableCollections binded to some WPF controls and they work fine. And I have a feature where I completely replace these ObservableCollections through reassignment and filling them again, but after doing this, the WPF controls don't get updated.

Or is this binding connection only established at startup once, and then I should never reinitialize the ObservableCollections, but only change them?

EDIT:

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
}

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">

           开发者_Python百科 <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>


The object you are binding to should implement the INotifyPropertyChanged interface. Then, the bound collection property should raise PropertyChanged event in its setter. Something like this:

public ObservableCollection<MyObject> MyCollection
{
   get
   {
      return _myCollection;
   }
   set
   {
      _myCollection = value;
      RaisePropertyChanged("MyCollection");
   }
} 


Try not to reassign, but clear and add new items.


You’ll need to know the dependency object and dependency property where the binding was defined. Then you can use this line:

BindingOperations.GetBindingExpressionBase(dependencyObject, dependencyProperty).UpdateTarget();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜