【问题标题】:WPF DataGrid selecting disabledWPF DataGrid 选择禁用
【发布时间】:2010-09-01 20:21:10
【问题描述】:

在 VS 2010/.NET 4.0 中,我有一个简单类的 List<MyClass> 列表:

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.SelectedItem 始终返回 null 的意义上,这可以正常工作。 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>

【问题讨论】:

  • 鉴于以上信息,DataGrid 应该可以正常工作(我试过了)。其他一些事情很可能导致这种情况。你能发布你的 XAML 吗?
  • 看到了你的 XAML。我尝试使用直接复制粘贴,它仍然可以正常工作。 i.imgur.com/ItS6V.png。如果您将鼠标悬停在列标题上,您会收到回复吗?有几件事:1)也许其他东西正在禁用网格?或 2) 网格顶部有一些透明的东西?我很确定它与网格的初始化方式无关。
  • 您的应用程序中定义的 DataGrids 是否有一些样式或 ControlTemplate(带有 x:Key="{x:Type DataGrid}")?然后您需要将此添加到您的问题中。
  • @karmic:如果网格上有透明的东西,它不会注册点击事件,它会注册... @Simpzon:DataGrid 没有样式,有一个针对 DataGridColumnHeader 的样式,但我当我将其注释掉时得到相同的结果...
  • 好的,问题是当它的父 Tab GotFocus 事件触发时我正在加载 DataGrid,并且每次单击 DataGrid 时都会触发此事件...不确定我是否应该关闭这个问题?

标签: .net wpf wpfdatagrid


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2011-12-12
    相关资源
    最近更新 更多