C# 将datagrid绑定到数据库中的数据时出现问题
我在wpf mvvm项目中使用datagrid时遇到了一个问题 这是我的xaml:C# 将datagrid绑定到数据库中的数据时出现问题,c#,wpf,mvvm,datagrid,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/edi
<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; }
}
}
namespace noteManager.ViewModel
{
公共类noteManagerViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
无效通知(字符串属性)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this,newpropertychangedventargs(property));
}
}
字符串登录;
int当前用户;
公共字符串登录
{
得到
{
返回登录;
}
设置
{
登录=值;通知(“登录”);
}
}
私人楼宇可执行;
public noteManagerViewModel()
{
_canExecute=true;
}
私有ICommand_testConnexion;
公共ICommand testConnexion
{
得到
{
返回_testConnexion???(_testConnexion=newcommandhandler(()=>Connexion(),_canExecute));
}
}
私有ICommand_addUser;
公共ICommand addUser
{
得到
{
返回_addUser???(_addUser=newcommandhandler(()=>AjoutUser(),_canExecute));
}
}
私有ObservableCollection_DataGridNotes=新ObservableCollection();
公共可观测集合dataGridNotes
{
//不需要公共设置器
获取{return\u 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; } }
}
公共类用户
{
/*公共用户()
{
this.Note=new HashSet();
}*/
公共int Id{get;set;}
公共字符串登录{get;set;}
//公共虚拟ICollection注释{get;set;}
}
公共类注释:INotifyPropertyChanged
{
公共int Id{get;set;}
公共字符串NoteText{get;set;}
公共字符串ContentText{get;set;}
public DateTime CreatedAt{get;set;}
公共日期时间?UpdatedAt{get;set;}
public int UserId{get;set;}
//公共虚拟用户用户{get;set;}
公共事件属性更改事件处理程序属性更改;
公共无效通知(字符串属性)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this,newpropertychangedventargs(property));
}
}
}
公共类DataGridNotes
{
私有字符串noteTitle;
私有字符串\u noteContent;
私有字符串_noteCreatedAt;
私有字符串_noteUpdatedAt;
公共字符串NoteTitle{get{return}NoteTitle;}set{{NoteTitle=value;}}
公共字符串NoteContent{get{return}NoteContent;}set{{NoteContent=value;}}
公共字符串NoteCreatedAt{get{return}NoteCreatedAt;}set{{NoteCreatedAt=value;}}
公共字符串NoteUpdatedAt{get{return}NoteUpdatedAt;}set{{NoteUpdatedAt=value;}}
}
抱歉,代码太难看了,对于c#来说是个新项目
我想在我的viewmodel中使用datagrid,但没有找到使其工作的方法(希望在datagrid中写入mysql数据库中的数据)
你有办法让它工作吗
thx提前好的,在没有查看ViewModel的情况下很难发现您做错了什么,但是您可能需要检查以下几点: 1) DataContext是正确的 2) 属性_DataGridNotes存在。请检查程序输出,确保没有警告通知您绑定已断开 您希望拥有的财产应如下所示:
List<Note> _DataGridNotes
{
get
{
// get notes from SQL request
// construct list of Note and return list
}
}
List\u DataGridNotes
{
得到
{
//从SQL请求获取注释
//构造注释列表和返回列表
}
}
您还应该确保Note类包含所需的属性(NoteTitle、NoteContent、NoteCreatedAt、NoteUpdatedAt)
如果问题出在对SQL数据库的请求中,那么可能还值得传回一些伪注释进行调试。问题在于您正试图绑定到一个私有的可观察集合\u DataGridNotes,您应该绑定到属性DataGridNotes:
ItemsSource="{Binding dataGridNotes}"
您好,thx对于如何使用datagrid仍然没有看到的帮助,我已经在我的第一篇文章中添加了信息!一些匿名用户建议进行以下编辑:“当您应该绑定到dataGridNotes(而不是_dataGridNotes)时,您似乎绑定到了一个私有的可观察集合。例如,
ItemsSource=“{binding dataGridNotes}”
”而另一个用户(@lagunag)刚刚提出了相同的编辑。@lagunag,如果你认为这个答案是错误的,只需将你的版本写为注释,甚至是完整的答案(如果太长或者你不能注释)。不要使用编辑功能更正其他人的帖子。您的VS输出窗口是否显示绑定错误?我猜它会抱怨找不到名为\u DataGridNotes
的属性。您已为公共属性DataGridNotes
定义了一个名为该属性的私有支持字段。请将绑定更改为ItemsSource=“{Binding dataGridNotes}”
。查看多个帐户是否存在问题。