从一个数据网格中选择数据将显示在另一个数据网格上

时间:2016-08-03 17:37:55

标签: c# wpf xaml data-binding datagrid

我正在尝试从数据网格中选择一个客户,然后将该客户名称显示在当天看到的客户列表中

这是XAML:

<Grid Name="gridListOfAllCustomers" Visibility="Collapsed" Margin="235,26,-740,56" Grid.Row="3" Grid.RowSpan="4" Grid.Column="1">
    <DataGrid Name="dataGridListOfAllCustomers" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Customer}" SelectionChanged="dataGridListOfAllCustomers_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Customer User Name" Binding="{Binding UserName}" Width="*"/>
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="*"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" Width="*"/>
            <DataGridTextColumn Header="Birthday" Binding="{Binding Birthday, StringFormat=\{0:dd/MM/yyyy\}}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

^该网格是我从

中选择数据的网格
<Grid Name="gridCustomersOfTheDay" Visibility="Collapsed" Margin="10,2,2,42" Grid.Row="4" Grid.RowSpan="4" Grid.ColumnSpan="2">
    <Grid.RowDefinitions>
        <RowDefinition Height="7*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <DataGrid Name="dataGridCustomersToday" IsReadOnly="True" AutoGenerateColumns="True" Grid.Row="0"   AutoGeneratingColumn="dataGridCustomersToday_AutoGeneratingColumn"/>
    <Button Name="btnEditCustomersToday" Content="Edit List" Grid.Row="1"/>
    <Button Name="btnClearCustomers"  Content="Clear List" Grid.Row="2" Click="btnClearCustomers_Click"/>
 </Grid>

^网格应显示数据

当我点击今天的客户按钮

时会发生这种情况
private void btnCustomersToday_Click(object sender, RoutedEventArgs e)
{
    if (btnCustomersToday.IsChecked == true)
    {
       dataGridListOfAllCustomers.Visibility = System.Windows.Visibility.Visible;
       dataGridCustomersToday.Visibility = System.Windows.Visibility.Visible;
    }
    else
    {
        dataGridListOfAllCustomers.Visibility = System.Windows.Visibility.Collapse;
        dataGridCustomersToday.Visibility = System.Windows.Visibility.Collapse;
    }

    ResponseBase<List<CustomerInfo>> allCustomers = Data.DataManager.GetCustomerList("");
    List<CustomerInfo> listOfAllCustomers = allCustomers.Data;
    dataGridListOfAllCustomers.ItemsSource = listOfAllCustomers; //Field
    dataGridCustomersToday.ItemsSource = displayWorkList; //Field
}

最后这是选择更改

private void  dataGridListOfAllCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{       
    CustomerInfo CustomerForTodayList = (CustomerInfo) dataGridListOfAllCustomers.SelectedItem;
    if (e.AddedItems.Count == 0)
    {
        return;
    }

    currentCustomersToday.Add(CustomerForTodayList.UserName);

    String customerID = CustomerForTodayList.UserName;
    StringValue addCustomer = new StringValue(customerID);

    displayWorkList.Add(addCustomer);


}

所以问题是我点击了客户,我必须更改第二个数据网格的可见性,以便显示客户的用户名。由于某种原因,第二个客户也存在滞后。当我选择第二个客户时,我还需要选择第三个客户才能出现(我仍然需要点击)。

我甚至不知道什么是可能的解决方案,因为我对数据绑定和数据网格的理解非常有限。我曾尝试阅读文档和阅读教程,但没有人彻底解释路径和来源以及如何制作集合等等。

0 个答案:

没有答案