0

我用包含对象DataGrid的绑定填充了一个。List<>我可以让它们出现在 中 DataGrid,但问题是,我无法选择一行。当我添加IsSynchronizedWithCurrentItem时,第一行似乎被选中(行的蓝色背景),但我无法更改它,甚至无法删除它。这是我DataGrid在 XAML 中的样子:

<Window x:Class="ClientTrackerWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="560" Width="594" KeyDown="Window_KeyDown">
    <Grid>
         <DataGrid AutoGenerateColumns="False" Height="231" HorizontalAlignment="Left" Margin="1,2,0,0" Name="datagridClients"
                  VerticalAlignment="Top" Width="500" HorizontalScrollBarVisibility="Disabled" SelectionChanged="datagridClients_SelectionChanged"
                  SelectionMode="Single" IsSynchronizedWithCurrentItem="True">
                <DataGrid.Columns>
                      <DataGridTextColumn Header="Prénom" MinWidth="175" Binding="{Binding Path=_prenom}" />
                      <DataGridTextColumn Header="Nom" MinWidth="175" Binding="{Binding Path=_nom}" />
                      <DataGridTextColumn Header="Téléphone" MinWidth="150" Binding="{Binding Path=_telephone}" />
                </DataGrid.Columns>
         </DataGrid>
     </Grid>
</Window>

我试着在这里和谷歌上寻找,但找不到对我有用的东西。顺便说一句,我正在使用 C#。

这是 C# 部分。

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace ClientTrackerWPF
    {
        /// <summary>
        /// Logique d'interaction pour MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            BDD bdd;
            List<Client> listeClients;
            bool started = false;

            public MainWindow()
            {
                InitializeComponent();
            }

            private void initilisationApp()
            {
                bdd = new BDD();
                bdd.startConnection("clients.sqlite");

                listeClients = new List<Client>();
                listeClients = bdd.getAllClients();
                datagridClients.ItemsSource = listeClients; //The source of the binding

                tabMain.IsEnabled = true;
                menuFileInit.Visibility = Visibility.Collapsed;
                menuFileNewClient.Visibility = Visibility.Visible;
                started = true;
            }

            private void menuFileInit_Click(object sender, RoutedEventArgs e)
            {
                initilisationApp();
            }
            private void datagridClients_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                Console.WriteLine(e.Source);
            }
        }
    }

谢谢 !

4

0 回答 0