将数据网格绑定到数据库中的数据时遇到问题

时间:2015-01-06 14:04:55

标签: c# wpf mvvm datagrid

我在wpf mvvm项目中使用datagrid时遇到问题

这是我的xaml:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
        x:Class="noteManager.MainWindow"
        xmlns:vm="clr-namespace:noteManager.ViewModel"
        DataContext="{StaticResource noteManagerViewModel}"
        Title="NoteManager" Height="490" Width="525">
    <Grid Margin="0,0,0,-132.5">
        <Grid.RowDefinitions>
            <RowDefinition Height="10"></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="200"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="110"></RowDefinition>
            <RowDefinition Height="111"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10"></ColumnDefinition>
            <ColumnDefinition Width="80"></ColumnDefinition>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition Width="80"></ColumnDefinition>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition Width="50"></ColumnDefinition>
            <ColumnDefinition Width="50"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Login :" FontSize="16" Grid.Column="2" Margin="51,9,50,0" Grid.RowSpan="2" Height="23" VerticalAlignment="Top" Grid.ColumnSpan="2"/>
        <TextBox Text="{Binding Login}" Grid.Row="1" Grid.Column="3" Margin="14,0,86,29" Grid.ColumnSpan="2"/>
        <Button Background="LightGreen" Foreground="Green" Command="{Binding testConnexion}" x:Name="testConnexion" Content="Connexion" Grid.Row="1" Grid.Column="2" Margin="51,29,86,0" Grid.ColumnSpan="3"/>
        <Button Command="{Binding addUser}" Content="+" Grid.Row="1" Grid.Column="4" Margin="34,1,20,0" RenderTransformOrigin="0.742,0.468"/>

        <DataGrid Name="dataGrid1"  Grid.Row="2" Margin="8,7,-22,7" AutoGenerateColumns="False" 
                  ItemsSource="{Binding _DataGridNotes}" SelectedItem="{Binding Path=MySelectedNote}" HorizontalAlignment="Center"
                  Width="480" Grid.ColumnSpan="6" Grid.Column="1">
            <DataGrid.Columns>
                <DataGridTextColumn Width="100" Binding="{Binding Path=NoteTitle}" Header="Titre" />
                <DataGridTextColumn Width="200" Binding="{Binding Path=NoteContent}" Header="Note" />
                <DataGridTextColumn Width="100" Binding="{Binding Path=NoteCreatedAt}" Header="Date de création" />
                <DataGridTextColumn Width="100" Binding="{Binding Path=NoteUpdatedAt}" Header="Dat MAJ" />
            </DataGrid.Columns>
        </DataGrid>

        <TextBlock Text="Titre" FontSize="16" Grid.Row="3" Grid.Column="1" Margin="27,8,7,1"/>
        <TextBox Text="{Binding Path=titre, Mode=TwoWay}" Grid.Row="3" Grid.Column="2" Margin="17,10,23,10" Grid.ColumnSpan="5"/>
        <TextBlock Text="Note" FontSize="16" Grid.Row="4" Grid.Column="1" Margin="27,4,7,0"/>
        <TextBox Text="{Binding Path=description, Mode=TwoWay}" Grid.Row="4" Grid.Column="2" Margin="17,10,23,8" Grid.ColumnSpan="5"/>
        <Button Command="{Binding Path=DeleteNote}" Background="LightPink" Foreground="red" Content="Supprimer" Grid.Row="5" Grid.Column="1" Margin="55,7,26,81" Grid.ColumnSpan="2"/>
        <Button Command="{Binding Path=UpdateANote}" Content="Mettre à jour" Grid.Row="5" Grid.Column="3" Margin="14,7,67,81" Grid.ColumnSpan="2" RenderTransformOrigin="0.5,0.5"/>
        <Button Command="{Binding Path=AddNote}" Content="Ajouter" Grid.Row="5" Grid.Column="4" Margin="78,7,10,81" Grid.ColumnSpan="3"/>
    </Grid>
</Window>

这是我的viewModel:

namespace noteManager.ViewModel
{
    public class noteManagerViewModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        void Notify(string property)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        string login;
        int currentUser;
        public string Login
        {
            get
            {
                return login;
            }
            set
            {
                login = value; Notify("Login");
            }
        }
        private bool _canExecute;

        public noteManagerViewModel()
        {
            _canExecute = true;
        }

        private ICommand _testConnexion;
        public ICommand testConnexion
        {
            get
            {
                return _testConnexion ?? (_testConnexion = new CommandHandler(() => Connexion(), _canExecute));
            }
        }

        private ICommand _addUser;
        public ICommand addUser
        {
            get
            {
                return _addUser ?? (_addUser = new CommandHandler(() => AjoutUser(), _canExecute));
            }
        }

        private ObservableCollection<DataGridNotes> _DataGridNotes = new ObservableCollection<DataGridNotes>();

        public ObservableCollection<DataGridNotes> dataGridNotes
        {
            // No need for a public setter
            get { return _DataGridNotes; }
        }
}

我使用的另一个类:

public class User
    {
        /*public User()
        {
            this.Note = new HashSet<Note>();
        }*/

        public int Id { get; set; }
        public string Login { get; set; }

        //public virtual ICollection<Note> Note { get; set; }
    }

    public class Note : INotifyPropertyChanged
    {
        public int Id { get; set; }
        public string NoteText { get; set; }
        public string ContentText { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime? UpdatedAt { get; set; }
        public int UserId { get; set; }

        //public virtual User User { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void Notify(string property)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
    public class DataGridNotes
    {
        private string _noteTitle;
        private string _noteContent;
        private string _noteCreatedAt;
        private string _noteUpdatedAt;

        public string NoteTitle { get { return _noteTitle; } set { _noteTitle = value; } }
        public string NoteContent { get { return _noteContent; } set { _noteContent = value; } }
        public string NoteCreatedAt { get { return _noteCreatedAt; } set { _noteCreatedAt = value; } }
        public string NoteUpdatedAt { get { return _noteUpdatedAt; } set { _noteUpdatedAt = value; } }
    }

对于丑陋的代码感到抱歉,对于项目来说,c#是新手。

我想在我的viewmodel中使用datagrid但是找不到让它工作的方法(想在datagrid中从mysql数据库写入数据

你有想法让它发挥作用吗?

事先提前

2 个答案:

答案 0 :(得分:1)

好的,如果没有看到ViewModel,很难发现你做错了什么,但是你可能想检查以下内容:

1)DataContext是正确的。

2)属性_DataGridNotes存在。检查程序输出以确保没有警告通知您绑定已损坏。

您希望拥有的房产应该是这样的:

List<Note> _DataGridNotes
{
    get
    {
        // get notes from SQL request
        // construct list of Note and return list
    }
} 

您还应该确保Note类包含所需的属性(NoteTitle,NoteContent,NoteCreatedAt,NoteUpdatedAt)。

如果问题出在对SQL数据库的请求中,也可能值得将一些虚拟注释传回调试。

答案 1 :(得分:0)

问题是您正在尝试绑定到私有Observable集合_DataGridNotes,您应该绑定到属性dataGridNotes:

ItemsSource="{Binding dataGridNotes}"