0

我是 WPF 的新手。我有一个带有多个选项卡的应用程序。在一个选项卡中,我可以将数据插入数据库中的表中。在另一个选项卡中,我有一个组合框,其中包含前面提到的表的 itemsource。当用户想从组合框中进行选择时,我想更新组合框项。/

我尝试通过以下方式使用 GotFocus 属性:

private void ComboBoxOperatingPoints_GotFocus_1(object sender, RoutedEventArgs e)
        {
            this.ThisViewModel.UpdateModel();
        }

Updatemodel 函数包含以下内容:

this.OperatingPoints = new ObservableCollection<Operating_Point>(new OperatingPointRepository().GetAll());
            this.NotifyPropertyChanged("OperatingPoints");

XAML 中的组合框绑定:

<ComboBox SelectionChanged="ComboBoxOperatingPoints_SelectionChanged" 
                      x:Name="ComboBoxOperatingPoints" 
                      GotFocus="ComboBoxOperatingPoints_GotFocus_1"
                      FontSize="30" 
                      HorizontalAlignment="Right" 
                      Margin="40,40,0,0" 
                      VerticalAlignment="Top" 
                      Width="200" 
                      Height="50"
                      IsSynchronizedWithCurrentItem="True"
                      ItemsSource="{Binding OperatingPoints}"
                      DisplayMemberPath="name"
                      SelectedValue="{Binding OperatingPointID,UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="operating_point_id"
                      >

组合框刷新,但出现验证错误,在第一个 GotFocus 事件发生后我无法再使用它。提前致谢!

编辑:

最后,我将 GotFocus 事件更改为 DropDownOpened 事件,它工作正常。

4

1 回答 1

0

ObservableCollection您的代码在每次更新时都会创建一个新代码。您可能只想创建ObservableCollection一次,然后将其内容替换为UpdateModel. 因此,例如,在您的视图模型的构造函数中,实例化OperatingPoints集合:

public class MyViewModel {

    public MyViweModel() {
        this.OperatingPoints = new ObservableCollection<Operating_Point>();
    }
}

然后,在UpdateModel

public void UpdateModel() {
    this.OperatingPoints.Clear();

    foreach ( Operating_Point point in new OperatingPointRepository().GetAll() ) {
        this.OperatingPoints.Add(point);
    }
    NotifyPropertyChanged( "OperatingPoints" );
}
于 2013-09-06T17:57:48.793 回答