datagrid 数据修改
通过datagrid的一列中的下拉列表,来确定datagrid另一列中的数据
数据源数据类型代码:
public class Phase : INotifyPropertyChanged {
private int _Code;
public int PhaseID { get; set; }
public int YellowFlashTime { get; set; }
public int GreenFlashTime { get; set; }
public int ShortestTime { get; set; }
public int LongestTime { get; set; }
public ObservableCollection<int> PhaseCode { get; set; }
public int Code {
get {
return _Code;
}
set {
_Code = value;
NotifyPropertyChanged("Code");
}
}
public Phase(int _PhaseID, int _YellowFlashTime, int _GreenFlashTime, int _ShortestTime, int _LongestTime) {
PhaseID = _PhaseID;
YellowFlashTime = _YellowFlashTime;
GreenFlashTime = _GreenFlashTime;
ShortestTime = _ShortestTime;
LongestTime = _LongestTime;
PhaseCode = new ObservableCollection<int>();
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
<dg:DataGridTemplateColumn Header="方向" Width="40">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<source:MyComboBox x:Name="OrientationSelect"
SelectedItem="{Binding Path=方向,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Source={StaticResource Orientation}}"
IsReadOnly="False"
DisplayMemberPath="Day" SelectedValuePath="Index"
SelectionChanged="CrossingName_SourceUpdated" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
<source:CustomColumn Header="相位代码" Width="70" x:Name="Code"
Binding="{Binding Code, Mode=TwoWay}">
</source:CustomColumn>
private void CrossingName_SourceUpdated(object sender, SelectionChangedEventArgs e) {
int index = Convert.ToInt32(((ComboBox)sender).SelectedValue)-1;
OrientationIndex = index;
CrossNameIndex = CrossingName.SelectedIndex;
var item = CrossingName.SelectedItem as Phase;
if (item != null) {
//MessageBox.Show(PhaseCode.GetType().ToString());
CustomColumn customColumn = new CustomColumn();
try {
//customColumn = new CustomColumn();
} catch (TypeInitializationException ex) {
Console.WriteLine(ex.ToString());
}
//改变数据源的数据
_DayPeriodList[DayPeriodIndex][PhasePeriodIndex].PhaseList[CrossingName.SelectedIndex].Code =
_DayPeriodList[DayPeriodIndex][PhasePeriodIndex].PhaseList[CrossingName.SelectedIndex].PhaseCode[index]; //customColumn.PhaseCode = Convert.ToString( _DayPeriodList[DayPeriodIndex][PhasePeriodIndex].PhaseList[CrossingName.SelectedIndex].PhaseCode[index]);
//CrossingName.ItemsSource = null;
//CrossingName.ItemsSource = _DayPeriodList[DayPeriodIndex][PhasePeriodIndex].PhaseList;
}
}