Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框刷新提供的值“”无法转换。验证错误_C#_.net_Wpf - Fatal编程技术网

C# 组合框刷新提供的值“”无法转换。验证错误

C# 组合框刷新提供的值“”无法转换。验证错误,c#,.net,wpf,C#,.net,Wpf,我是WPF的新手。我有一个带有多个选项卡的应用程序。在一个选项卡中,我可以将数据插入数据库的表中。在另一个选项卡中,我有一个组合框,其中包含前面提到的表的itemsource。当用户想要从组合框中进行选择时,我想更新组合框项目/ 我以以下方式尝试了GotFocus属性: private void ComboBoxOperatingPoints_GotFocus_1(object sender, RoutedEventArgs e) { this.ThisV

我是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中的组合框bindind:

<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事件更改为DropDownOpen事件,它工作正常。

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

public class MyViewModel {

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

我确信我使用了错误的事件,因为如果我更改选项卡,刷新工作正常。但是,如果我单击组合框选择一个项目,我可以在分析器中看到select语句,该语句从数据库多次运行,直到缓存为止。您能为我推荐另一个刷新viewmodel的事件吗?我尝试了以下操作,当我单击组合框时,Get focus功能似乎运行了两次:private void组合框OperatingPoints\u GotFocus\u 1object sender,RoutedEventTargets e{//this.ThisViewModel.UpdateModel;e.Handled=true;}@TonyVitabile请不要,如果您更新了ObservableCollection,而不是将其替换为新实例,则无需自己触发NotifyChanged事件-ObservableCollection将执行此操作。但据我所知,它将为每一个操作提供支持,例如添加一个元素,这将导致大量GUI更新。因此,有时替换整个集合并为此触发NotifyChanged事件是更好的解决方案。@LóriNóda:我不知道为什么要两次获取该事件,尽管我怀疑这可能与气泡路由有关。只有在将新项插入到基础表或修改现有项时,才可能考虑更新组合框中的项。可以通过创建一个事件来实现这一点,该事件是在执行修改模型的操作时引发的,然后添加一个更新集合的处理程序。
public void UpdateModel() {
    this.OperatingPoints.Clear();

    foreach ( Operating_Point point in new OperatingPointRepository().GetAll() ) {
        this.OperatingPoints.Add(point);
    }
    NotifyPropertyChanged( "OperatingPoints" );
}