【问题标题】:How to bind a new rowAdd even to an ObservableCollection item如何将新的 rowAdd 绑定到 ObservableCollection 项
【发布时间】:2018-03-18 12:44:20
【问题描述】:

我有一个DataGrid 绑定到一个ObservableCollection,用户可以添加/删除行。在第一列中,当用户双击单元格时,应该会出现一个新窗口,但直到他离开单元格然后重新输入它才会出现。 调试后发现DataGridTextColumn直到第二次双击才初始化的问题。 我的绑定有什么问题?

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());
    }
}

【问题讨论】:

    标签: wpf data-binding datagridview observablecollection


    【解决方案1】:

    这里有一个竞争条件。 由于 DataGrid 不是只读的,当您双击一个单元格时,它会将其 DataTemplate 从显示模板更改为编辑模板。

    在 DataGridTextColumn 的 Display DataTemplate 中只有 TextBlock 元素。

    在 DataGridTextColumn 的 Edit DataTemplate 中有 TextBox 元素。

    你会很难捕捉到这样的 TextBox 元素。

    更好的选择是在行上有双击事件处理程序,并使用它的 DataContext 来提取您想要的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2014-01-12
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多