数据网格:用户无法编辑或选择行

时间:2013-05-09 17:21:21

标签: c# wpf datagrid edit

我在WPF中使用Datagrid来显示从List中的LINQ-SQL表达式返回的用户行:

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事件,只能捕获右键单击,但从不左键单击。

Datagrid的XAML是:

<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表是创建TextBlock而不是TexBox,但SQL Server中的表也看起来相同。

2 个答案:

答案 0 :(得分:2)

I experienced this same issue and found that each time a click is performed on a control that is within a tab that the "SelectionChanged" event fires for that tab. I put in a check in the code behind for the "SelectionChanged" handler to make sure the code only executes once per tab entry.

答案 1 :(得分:0)

周末休息一下,今天早上又回到了数据网格问题。我的应用程序基于用户可以在其间循环的多个选项卡,因此我尝试在单独的选项卡上托管datagrid并从MainWIndow构造函数填充datacontext。这很有效,所以我尝试从构造函数中加载原始的datagrid,它也可以工作。

原来问题在于调用填充数据网格的LINQ的选项卡的“Got Focus”属性。以下是代码:

<TabItem Header="Admin" Name="admin_tab" GotFocus="admin_tab_GotFocus" >
            <Grid>
                <Grid Name="grdAminTab" Margin="0,0,710,-1">        
                    <Button Content="Save" Height="23" Name="saveNewUser_BTN" Width="75" Click="saveUser_BTN_Click" HorizontalAlignment="Left" Margin="177,298,0,0" VerticalAlignment="Top" />
                    <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="6,298,0,0" Name="deleteUser_BTN" VerticalAlignment="Top" Width="75" Click="deleteUser_BTN_Click" />
                    <DataGrid AutoGenerateColumns="True" Margin="6,6,6,215" Name="users_DataGrid" 
                              CanUserAddRows="True" ItemsSource="{Binding}" Height="286" Width="246" />
                </Grid>
            </Grid>
        </TabItem>

之后:

<TabItem Header="Faculty" IsEnabled="False" Name="faculty_tab" />
        <TabItem Header="Admin" Name="admin_tab" >
            <Grid>
                <Grid Name="grdAminTab" Margin="0,0,710,-1">        
                    <Button Content="Save" Height="23" Name="saveNewUser_BTN" Width="75" Click="saveUser_BTN_Click" HorizontalAlignment="Left" Margin="177,298,0,0" VerticalAlignment="Top" />
                    <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="6,298,0,0" Name="deleteUser_BTN" VerticalAlignment="Top" Width="75" Click="deleteUser_BTN_Click" />
                    <DataGrid AutoGenerateColumns="True" Margin="6,6,6,215" Name="users_DataGrid" 
                              CanUserAddRows="True" ItemsSource="{Binding}" Height="286" Width="246" />
                </Grid>
            </Grid>
        </TabItem>

在用户打开该标签之前,不确定是否有理由加载数据网格是一种不好的做法,但现在它解决了我的问题!任何有关此事件处理程序无法正常工作的见解将不胜感激。我遇到类似的问题,EventArgs对于鼠标事件不正确,也许这是相似的?