WPF DataGrid 选择禁用

发布于 2024-09-17 12:33:38 字数 2858 浏览 3 评论 0原文

在 VS 2010/.NET 4.0 中,我有一个简单类的 List 列表,如下所示:

public class MyClass
{        
    public string String1 { get; set; }
    public string String2 { get; set; }

    public MyClass(string string1, string string2)
    {
        String1 = string1;
        String2 = string2;
    }
}

此列表显示在代码后面的 WPF DataGrid

:首先添加:

List<DataGridColumn> cols = ParentClass.GetColumns();
foreach (DataGridColumn dgc in cols)
{
    myDataGrid.Columns.Add(dgc);
}

设置项目源 接下来:

myDataGrid.ItemsSource = ParentClass.MyClassess;

MyClassess 是列表

这在 DG 被填充但无法在 DataGrid 中选择行且始终返回的情况下工作正常。无效的。 DataGrid 注册鼠标事件,但无论何时发生单击,都不会选择任何行,就好像 DataGrid 被禁用一样...

SelectionMode="Single"  
SelectionUnit="FullRow" 
IsReadOnly="False"

有人知道这里发生了什么吗?

TIA

以下是所要求的 XAML:

<TabItem Header="Sessions" GotFocus="TabSessionsGotFocus">
                    <Grid>
                        <Grid.RowDefinitions>                            
                            <RowDefinition Height="*"></RowDefinition>                            
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <DockPanel Grid.Row="0" Grid.ColumnSpan="3" Margin="0">
                            <DataGrid x:Name="dgSessions" DockPanel.Dock="Top" IsEnabled="True" AutoGenerateColumns="False" 
                                SelectionMode="Single"  SelectionUnit="FullRow" Background="WhiteSmoke" 
                                AlternatingRowBackground="Gainsboro" GridLinesVisibility="None" IsReadOnly="False"
                                ColumnHeaderHeight="25" RowHeight="21" MouseRightButtonDown="SessionsRightButtonDown" >
                                <DataGrid.ContextMenu>
                                    <ContextMenu x:Name="cmSessions">
                                        <MenuItem x:Name="miSessionActions">
                                            Action
                                        </MenuItem>                                        
                                    </ContextMenu>
                                </DataGrid.ContextMenu>
                            </DataGrid>
                        </DockPanel>
                    </Grid>
                </TabItem>

In VS 2010/.NET 4.0, I have a List<MyClass> list of a simple class as such:

public class MyClass
{        
    public string String1 { get; set; }
    public string String2 { get; set; }

    public MyClass(string string1, string string2)
    {
        String1 = string1;
        String2 = string2;
    }
}

This list is displayed in a WPF DataGrid in code behind:

Columns are added first:

List<DataGridColumn> cols = ParentClass.GetColumns();
foreach (DataGridColumn dgc in cols)
{
    myDataGrid.Columns.Add(dgc);
}

Item source is set next:

myDataGrid.ItemsSource = ParentClass.MyClassess;

MyClassess is the List

This works fine in a sense that the DG gets populated but it is not possible to select a row in the DataGrid and DataGrid.SelectedItem always returns null. The DataGrid registers mouse events but where ever the click occurs no row gets selected as if the DataGrid is disabled...

SelectionMode="Single"  
SelectionUnit="FullRow" 
IsReadOnly="False"

Does anyone have any idea what is going on here?

TIA

Here is the XAML as requested:

<TabItem Header="Sessions" GotFocus="TabSessionsGotFocus">
                    <Grid>
                        <Grid.RowDefinitions>                            
                            <RowDefinition Height="*"></RowDefinition>                            
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <DockPanel Grid.Row="0" Grid.ColumnSpan="3" Margin="0">
                            <DataGrid x:Name="dgSessions" DockPanel.Dock="Top" IsEnabled="True" AutoGenerateColumns="False" 
                                SelectionMode="Single"  SelectionUnit="FullRow" Background="WhiteSmoke" 
                                AlternatingRowBackground="Gainsboro" GridLinesVisibility="None" IsReadOnly="False"
                                ColumnHeaderHeight="25" RowHeight="21" MouseRightButtonDown="SessionsRightButtonDown" >
                                <DataGrid.ContextMenu>
                                    <ContextMenu x:Name="cmSessions">
                                        <MenuItem x:Name="miSessionActions">
                                            Action
                                        </MenuItem>                                        
                                    </ContextMenu>
                                </DataGrid.ContextMenu>
                            </DataGrid>
                        </DockPanel>
                    </Grid>
                </TabItem>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2024-09-24 12:33:38

问题是,当 DataGrid 的父 Tab GotFocus 事件触发时,我正在加载 DataGrid,并且每次单击 DataGrid 时都会触发该事件。

The problem was that I was loading the DataGrid when it's parent Tab GotFocus event fired and this event was firing every time the DataGrid was clicked on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文