簡體   English   中英

WPF UI不更新

[英]WPF UI not updating

我迷路了...我檢查了我想到的一切..

  1. DataContext已設置!
  2. DebugOutputWindow中沒有BindingErrors
  3. INotifyPropertyChanged已正確實現(因為Button-Clicks可以正常工作...)
  4. 集合已更新(計數> 0)

但是我的組合框中的項目沒有顯示。

還有什么可能是錯的?

這是我的代碼:

private void Refresh(bool isAuto)
{
        List<DatabaseEntry> entries = GetBrokenJobs(isAuto);

        Collection.Clear();
        foreach (string jobName in entries.Select(x => x.Description).ToList())
        {
            Collection.Add(jobName);
        }
}

我綁定到ComboxBox:

ItemsSource="{Binding Path=Collection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

private ObservableCollection<string> _collection = new ObservableCollection<string>();
public ObservableCollection<string> Collection
{
    get {return _collection; }
    set
    {
        _collection = value;
        OnPropertyChanged();//<= Has [CallerMemberName] in constructor ...
    }
}

我像往常一樣設置DataContext ..

this.DataContext = MainViewModel.Instance;

由於我所有的Buttons和CheckBoxes都可以正常工作,因此這可能不是源。

編輯

這是更多的Xaml

 <ComboBox x:Name="ComboBox"
                      Grid.Row="1"
                      Margin="10"                          
                      Grid.Column="1" 
                      Width="230"
                      VerticalContentAlignment="Center"
                      HorizontalContentAlignment="Center"
                      IsEnabled="{Binding Path=IsJobSelectorEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                      SelectedItem="{Binding Path=SelectedJobItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      ItemsSource="{Binding Path=Collection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            </ComboBox>

解決方案很簡單。 將組合框項目源綁定到ObservableCollection。

這不是必需的ItemsSource =“ {Binding Path = Collection,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}”

請像這樣使用ItemsSource =“ {Binding ComboBoxList}” ,其中ComboBoxList是在數據上下文中定義的可觀察集合。

我知道這有點與observablecollection應該做的事情背道而馳,但是過去我遇到了一個問題,即完全忽略添加到現有ObservableCollection中。

如果您嘗試替換整個observablecollection,以免發生類似情況,則可能會強制對其進行更新:

列表條目= GetBrokenJobs(isAuto);

集合=新的ObservableCollection(entries.Select(x => x.Description).ToList());

暫無
暫無

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

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