WPF DataGrid 选择禁用
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,当 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.