簡體   English   中英

如何在WPF中刷新綁定?

[英]How to refresh the binding in 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
}

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>

要綁定的對象應實現INotifyPropertyChanged接口。 然后,綁定的collection屬性應該在其setter中引發PropertyChanged事件。 像這樣的東西:

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

盡量不要重新分配,但要清除並添加新項目。

您需要知道定義綁定的依賴項對象和依賴項屬性。 然后你可以使用這一行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM