我使用一个DataGrid WPF中显示用户的行从列表中的LINQ-SQL表达式返回:的Datagrid:用户无法编辑或选择一行
private void admin_tab_GotFocus(object sender, RoutedEventArgs e)
{
if (dbMethods.CheckDatabaseConnection())
{
using (PubsDataContext db = new PubsDataContext())
{
var Allusers = new List<Application_User>(from users in db.Application_Users
select users);
users_DataGrid.DataContext = Allusers;
}
}
}
的用户加载就好了,排所有信息保持不变。但是,当左击行时,该行不会被选中,并且看起来没有传递任何事件。我添加了一个MouseDown事件,只能捕获右键单击,但从来没有左键单击。
的XAML为DataGrid是:
<DataGrid AutoGenerateColumns="True" Margin="6,6,6,215" Name="users_DataGrid"
CanUserAddRows="True" ItemsSource="{Binding}" Height="286" Width="246"
MouseLeftButtonDown="users_DataGrid_MouseLeftButtonDown" MouseDown="users_DataGrid_MouseDown"/>
这是相同的代码我有用于在不同的窗口的单独数据网格,这两个XAML和C#。下面是比较该样本:
XAML
<DataGrid AutoGenerateColumns="True" Margin="1,136,1,207" Name="studentLearningExperiences_DataGrid" ItemsSource="{Binding}"
HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" CanUserAddRows="True">
<DataGrid.ContextMenu>
<ContextMenu Name="studentLearningExperiences_ContextMenu">
<!--HACK BUG - currently does not work with the button click <MenuItem Header="Save" Click="learningExperienceSave_BTN_Click" /> -->
<MenuItem Header="Delete" Click="Delete_MenuItem_Click" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
C#
public void LoadStudentLearningExperiences()
{
if (dbMethods.CheckDatabaseConnection())
{
using (PubsDataContext db = new PubsDataContext())
{
var completionList = new List<Learning_Experience>(from s in db.Learning_Experiences
where s.Student_ID == student.Student_ID
select s);
studentLearningExperiences_DataGrid.DataContext = completionList;
}
}
}
任何人都可以找到,如果我有一个错误,错过了一些东西,或者从他们的经验建议?我已经引用这个链接Cannot edit cells,我想知道如果我的SQL Server表正在创建TextBlocks而不是TexBoxes,但SQL Server中的表也显示完全相同。
在这里找到了类似的问题[link](http://stackoverflow.com/questions/14955601/wpf-datagrid-cannot-add-a-row-after-binding-to-collection-with-data) – 2013-05-09 22:40:29