我有 2 个组合框。如果我在第一个活动中选择一个活动,则相关的子活动应显示在第二个组合框中。根据 MVVM 样式,代码看起来不错,但是当我在第一个活动中选择一个活动时,第二个组合框中的相关子活动不会同步。这是我的代码: 查看
<Window x:Class="TestDGCombosBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestDGCombosBinding"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="ActivitiesDataProvider" ObjectType="{x:Type local:Activities}" MethodName="GetActivities"/>
<local:DebugConverter x:Key="DebugConverter" />
</Grid.Resources>
<DataGrid
Grid.Row="1" Grid.Column="1"
AutoGenerateColumns="False"
SelectionUnit="CellOrRowHeader"
SelectionMode="Single"
IsSynchronizedWithCurrentItem="True"
RowBackground="White"
AlternatingRowBackground="LightGray"
AlternationCount="2" Name="dataGrid1" CurrentCellChanged="dataGrid1_CurrentCellChanged">
<DataGrid.BindingGroup>
<BindingGroup />
</DataGrid.BindingGroup>
<DataGrid.Resources>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Activities Custom" CanUserSort="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Text="{Binding CurrentActivity}"
SelectedValuePath="ActivityID"
DisplayMemberPath="ActivityName"
ItemsSource="{Binding Source={StaticResource ActivitiesDataProvider}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="SubActivities Custom" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Text="{Binding CurrentSubActivity}"
SelectedValuePath="SubActivityID"
DisplayMemberPath="SubActivityname"
ItemsSource="{Binding Path=SubActivitiesOfCurrentActivity}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
模型
public class Activities
{
public DataView GetActivities()
{
return ActivitiesAccess.GetAllActivities();
}
}
视图模型
public class ActivitiesViewModel : INotifyPropertyChanged
{
public ActivitiesViewModel()
{ }
private int currentActivity;
public int CurrentActivity
{
get { return currentActivity; }
set {
currentActivity = value;
SubActivitiesOfCurrentActivity = ActivitiesAccess.GetAllSubActivitiesinActivity(currentActivity);
OnPropertyChanged("CurrentActivity");
}
}
private DataView subActivitiesOfCurrentActivity;
public DataView SubActivitiesOfCurrentActivity
{
get {return subActivitiesOfCurrentActivity; }
set
{
subActivitiesOfCurrentActivity = value;
OnPropertyChanged("SubActivitiesOfCurrentActivity");
}
}
private int currentSubActivity;
public int CurrentSubActivity
{
get { return currentSubActivity; }
set
{
currentSubActivity = value;
OnPropertyChanged("CurrentSubActivity");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
当我从第一个下拉列表中选择一个活动时调试代码时,CurrentActivity 属性值始终为 0,我不知道为什么。此值应等于所选活动的 ActivityID。我搜索了很多以寻求帮助,但我找不到任何东西。如果有人可以在代码中指定任何问题,我将非常高兴。