【问题标题】:Selecting first row of datagrid when WPF form is loaded加载 WPF 表单时选择数据网格的第一行
【发布时间】:2020-12-04 19:14:00
【问题描述】:

在 WPF 窗口中,我有一个绑定到 DataGrid 的文本框,该文本框已经填充了数据。当我选择不同的行时,文本框中的文本会正确更改。 但是,当 WPF 加载时,一开始没有选择,所以文本框保持为空。只有当我实际选择一行时,才会绑定到文本框。

如何实现在加载表单时自动选择 DataGrid 的第一行并填充文本框。

这是我将文本框绑定到数据网格 (XAML) 的方式:

<TextBox x:Name="txtContactNaam" Grid.Column="1" Grid.Row="11" Grid.ColumnSpan="1" Margin="10,10,10,10" Text  = "{Binding ElementName = dgContacten, Path = SelectedItem.[Naam], 
     Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" ></TextBox>

感谢您的帮助。

更多代码:

public partial class wpfTest_2 : Window

{ 数据表 dtContacten;

DataSet dsContacten = new DataSet("Contacten");

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    MaakTabellen();
    VulTabellen();

    dgContacten.ItemsSource = dtContacten.DefaultView;
}

private void MaakTabellen()
{
    dtContacten = new DataTable("Contacten");
    dsContacten.Tables.Add(dtContacten);

    dtContacten.Columns.Add("Naam", typeof(string));
    dtContacten.Columns.Add("Voornaam", typeof(string));
}

private void VulTabellen()
{
    ToevoegenDataContacten("Vanderbeke", "Michel");
    ToevoegenDataContacten("Maes", "Maddy");
    ToevoegenDataContacten("Pieters", "Jan");
}

private void ToevoegenDataContacten(string naam, string voornaam)
{
    DataRow nieuwContact = dsContacten.Tables["Contacten"].NewRow();

    nieuwContact["Naam"] = naam;
    nieuwContact["Voornaam"] = voornaam;

    dsContacten.Tables["Contacten"].Rows.Add(nieuwContact);
}

}

<DataGrid x:Name="dgContacten" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="6" Grid.RowSpan="10" Margin="10,10,10,10" RowBackground="LightYellow" AlternatingRowBackground="LightBlue" ItemsSource="{Binding Table}" 
                 SelectedItem="{Binding Row, Mode=TwoWay}"></DataGrid>

<TextBox x:Name="txtContactNaam" Grid.Column="1" Grid.Row="11" Grid.ColumnSpan="1" Margin="10,10,10,10" Text  = "{Binding ElementName = dgContacten, Path = SelectedItem.[Naam], 
     Mode = TwoWay, UpdateSourceTrigger =PropertyChanged}" ></TextBox>
    <TextBox x:Name="txtContactVoornaam" Grid.Column="1" Grid.Row="12" Grid.ColumnSpan="1" Margin="10,10,10,10" Text  = "{Binding ElementName = dgContacten, Path = SelectedItem.[Voornaam],
     Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}"></TextBox>

希望这能澄清我的问题。

【问题讨论】:

  • 请添加DataGrid xaml,以及填充数据的代码

标签: c# wpf


【解决方案1】:

您可以在分配 ItemsSource 后设置选定的行:

dgContacten.ItemsSource = dtContacten.DefaultView;
dgContacten.SelectedIndex = 0;

顺便说一句,以下绑定没有做任何事情:

ItemsSource="{Binding Table}" 
SelectedItem="{Binding Row, Mode=TwoWay}"

因为

  • 您在代码中重置了 ItemsSource
  • 你没有分配 DataContext
  • 属性表和行不存在

【讨论】:

  • 是的,我已经尝试过 dgContacten.SelectedIndex = 0;
  • 是的,我已经尝试过 dgContacten.SelectedIndex = 0;但这无助于在加载表单时选择 DataGrid 的第一行。我仍然需要单击一行才能使数据出现在文本框中。你提到的绑定,我已经从我的 XAML 代码中删除了它们。问题仍然存在:如何在 form_load 之后自动选择一行?
猜你喜欢
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多