数据绑定2组合问题 [英] Databinding 2 comboboxes Issue
问题描述
我有2个组合框。如果我在第一个选择一个活动,相关的子活动应该显示在第二个组合框中。
代码看起来像每个MVVM样式,但是当我选择第一个活动,第二个组合框中的相关子活动不会同步。
这是我的代码:
查看
I have 2 comboboxes. If i select a Activity in the 1st one, the related sub activities should be displayed in the second combobox. The code looks fine as per MVVM style but when i select an activity on 1st one, the related subactivities in the 2nd combobox are not synched. Here is my code: View
<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();
}
}
ViewModel
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。
我搜索alot获得帮助,但我找不到任何东西。
如果有人可以在代码中指定任何问题,我会很高兴。
When i debug the code when i select an activity from the 1st drop down, the CurrentActivity property value is always 0 which im not sure why. This value should be equal to the ActivityID of the selected activity. I searched alot to get help but i couldnt find anything. I will be very happy if someone could specify any problem in the code.
推荐答案
我解决了我的问题。
当活动更改时,应通知相关子活动的更改。
但是当子活动更改时,没有必要通知活动。
I resolved my issue. When An Activity is Changed, the related Sub-Activities should be notified of the change. But when the Sub-Activity is changed there is no need to notify the Activity.
我只是在CurrentSubActivity属性中调用OnPropertyChanged()。
这里是代码
I just commented OnPropertyChanged() call in CurrentSubActivity property. Here is the code
private int currentSubActivity;
public int CurrentSubActivity
{
get { return currentSubActivity; }
set
{
//OnPropertyChanged("CurrentSubActivity");
this.currentSubActivity = value;
}
}
我希望这可以帮助某人太:)
I hope this helps someone out there too :)
这篇关于数据绑定2组合问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!