0
我有一個DataGrid
綁定到ObservableCollection
,用戶可以添加/刪除行。在第一列中,當用戶雙擊單元格時,會出現一個新窗口,但是直到他離開單元格然後重新輸入它纔會發生。 調試完成後,我發現DataGridTextColumn
直到第二次雙擊纔得到初始化。 我的綁定有什麼問題?如何綁定新行甚至添加到ObservableCollection項目
XAML:
<Window.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="MouseDoubleClick" Handler="CellDoubleClick"/>
</Style>
</Window.Resources>
<DataGrid x:Name="TeachersDataGrid" ItemsSource="{Binding TeacherInfoList, IsAsync=True}" FlowDirection="RightToLeft" Canvas.Left="104" Canvas.Top="18" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" Background="#FFEEECEC">
<DataGrid.Columns>
<DataGridTextColumn Header="الرقم" Binding="{Binding Path=Num, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="لقب الموظف و إسمه" IsReadOnly="True" Binding="{Binding Path=FullName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="مهنته" IsReadOnly="True" Binding="{Binding Path=CurrentStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="المؤسسة أو مكان العمل" IsReadOnly="True" Binding="{Binding Path=SchoolName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="الولاية" IsReadOnly="True" Binding="{Binding Path=Province, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="طبيعة التفتيش" Binding="{Binding Path=Notes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
C#:
private void CellDoubleClick(object sender, RoutedEventArgs e)
{
DataGridCell cell = null;
TextBox textBox = null;
cell = sender as DataGridCell;
if (cell == null)
//if (cell == null || (cell.Column.DisplayIndex == 0 || cell.Column.DisplayIndex == 5))
{
return;
}
MessageBox.Show("cell66");
DataGridRow row = DataGridRow.GetRowContainingElement(cell);
int rowIndex = row.GetIndex();
//MessageBox.Show(rowIndex.ToString());
textBox = cell.Content as TextBox;
if (textBox == null)
{
return;
}
MessageBox.Show("textBox");
try
{
TeachersListWindow TeachersListW = new TeachersListWindow(this, rowIndex);
TeachersListW.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
一個簡單的代碼片段會很棒。 –