C# 如何在MVVM WPF C的组合框中显示ObservaBleCollection中的值#
我在ViewModel类中获取可观察集合中的值,但未获取组合框中显示的值请帮助 我参考了这个链接 XAML App.xaml.csC# 如何在MVVM WPF C的组合框中显示ObservaBleCollection中的值#,c#,wpf,mvvm,C#,Wpf,Mvvm,我在ViewModel类中获取可观察集合中的值,但未获取组合框中显示的值请帮助 我参考了这个链接 XAML App.xaml.cs public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var mainWindow = new W
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var mainWindow = new Window();
var viewModel = new ViewModel();
mainWindow.DataContext = viewModel;
viewModel.GetCourseIdFromDB();
mainWindow.Show();
//viewModel.GetCourseIdFromDB();
}
}
不要将
Grid
的DataContext
设置为ViewModel
资源,如下所示:
<Grid DataContext="{Binding Source={StaticResource ViewModel}}">
同时删除资源本身:
<viewmodel:ViewModel x:Key="ViewModel"/>
我尝试了这一点,这一点满足了我的要求,即使用“-”分隔值将可观察集合中的数据显示到组合框中
<ComboBox Grid.Column="1" ItemsSource="{Binding FillCourseId}" Name="cmb_CourseIDName" HorizontalAlignment="Left" Margin="164.191,5,0,0"
Grid.Row="3" VerticalAlignment="Top" Width="168.342" Grid.RowSpan="2" DataContext="{Binding Source={StaticResource ViewModel}}" FontSize="8" IsReadOnly="True"
SelectedValue="{Binding Student.SCourseIDName,Mode=TwoWay}" RenderTransformOrigin="1.431,0.77">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=CourseName, Mode=TwoWay}" FontSize="12"/>
<TextBlock Text=" - "/>
<TextBlock Text="{Binding Path=CourseID, Mode=TwoWay}" FontSize="12"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我在ViewModel类的构造函数中调用了函数GetCourseIdFromDB()。谢谢你指出这一点,它真的解决了我的问题
public ViewModel()
{
Student = new Student();
Students = new ObservableCollection<Student>();
Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);
GetCourseIdFromDB();
}
公共视图模型()
{
学生=新学生();
学生=新的可观察集合();
Students.CollectionChanged+=新系统.Collections.Specialized.NotifyCollectionChangedEventHandler(Students\U CollectionChanged);
GetCourseIdFromDB();
}
在我看来,似乎没有人调用GetCourseIdFromDB()
,尝试将此函数称为构造函数,并查看问题是否得到解决,如果没有,请检查是否有任何数据来自DB@styx我已经调用了GetCourseIdFromDB()在app.xaml.cs中,在我的屏幕加载后,该函数会被触发。您是否也可以从app.xaml.cs发布此代码?@Ramco:您如何以及在哪里调用GetCourseIdFromDB()
?请用详细信息更新您的问题。您很可能正在视图模型类的另一个实例上调用该方法。@mm8:我正在调用函数app.xaml.cs。关键是我要在ViewModel和DataSource中获取视图中的值。在不同的实例中调用函数不会有问题,除非您在ViewModel和View中获得值。唯一的问题是我没有显示我的值。谢谢你指出这一点。我试过你的方法,但没用。相反,我在ViewModel类的构造函数中调用了该函数。
<viewmodel:ViewModel x:Key="ViewModel"/>
<ComboBox Grid.Column="1" ItemsSource="{Binding FillCourseId}" Name="cmb_CourseIDName" HorizontalAlignment="Left" Margin="164.191,5,0,0"
Grid.Row="3" VerticalAlignment="Top" Width="168.342" Grid.RowSpan="2" DataContext="{Binding Source={StaticResource ViewModel}}" FontSize="8" IsReadOnly="True"
SelectedValue="{Binding Student.SCourseIDName,Mode=TwoWay}" RenderTransformOrigin="1.431,0.77">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=CourseName, Mode=TwoWay}" FontSize="12"/>
<TextBlock Text=" - "/>
<TextBlock Text="{Binding Path=CourseID, Mode=TwoWay}" FontSize="12"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public ViewModel()
{
Student = new Student();
Students = new ObservableCollection<Student>();
Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);
GetCourseIdFromDB();
}