c# - 单击第一个数据网格行不触发 selectedIndex 更改
问题描述
我有一个数据网格:
<TabControl Grid.Row="3" VerticalAlignment="Stretch" Grid.ColumnSpan="2" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch">
<TabItem Header="Presentation">
<DataGrid ItemsSource="{Binding Path=CombinedPresentation , Mode=TwoWay}"
SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ObjectName" Binding="{Binding ObjectName}"></DataGridTextColumn>
<DataGridTextColumn Header="bActivated" Binding="{Binding bActivated}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</TabItem>
<TabItem Header="Presentation Piece" IsEnabled="{Binding Path=PieceEnabled}">
<DataGrid ItemsSource="{Binding Path=CombinedPresentationPiece , Mode=TwoWay}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</TabItem>
</TabControl>
SelectedIndex 绑定:
public int SelectedIndex
{
get
{
return _selectedIndex;
}
set
{
if (value == -1)
{
PieceEnabled = false;
_selectedIndex = value;
OnPropertyChanged();
}
else
{
PieceEnabled = true;
_selectedIndex = value;
OnPropertyChanged();
DoSomeLogic();
}
}
}
private int _selectedIndex = 0;
并启用第二个选项卡:
private bool _pieceEnabled= false;
public bool PieceEnabled
{
get
{
return _pieceEnabled;
}
set
{
_pieceEnabled = value;
OnPropertyChanged();
}
}
数据网格的数据源
private List<CombinedPresentationDto> _combinedPresentation;
public List<CombinedPresentationDto> CombinedPresentation
{
get
{
return _combinedPresentation;
}
set
{
_combinedPresentation = value;
OnPropertyChanged();
}
}
归档数据源:
private void GetPresentation()
{
try
{
string query = @"Select * from testTable";
Presentation = dataContext.ExecuteQuery<PresentationDto>(query, new string[] { }).ToList();
if (Presentation.Count > 0 && GetPresentationPieces())
{
CombinedPresentation = PresentationCombiner.CombinePresentations(Presentation, PresentationPiece);
}
}
catch (Exception ex)
{
}
}
PresentationCombiner 我只是在 Presentation 和 PresentationPiece 之间找到对并将它们合并到一个对象中。
我的问题是例如 Datagrid 显示两行。如果用户选择其中一行,则应启用第二个选项卡(它显示连接到所选行的数据)。但选择第一行不会触发selectedindex
。用户需要先选择第二行然后选择第一行才能触发选择第一行
解决方案
由于无法看到填充 DataGridView 的代码,我猜您已经选择了第一行,因此单击它不会更改 SelectedIndex。确保在填充 DataGridView 后没有选择行。
推荐阅读
- python - 检查清单的问题 - python模块部署
- sql - SQL 选择基于另一列的过滤列
- java - Spring Boot 中的动态 Bean 配置和加载
- ruby - 成功安装 gem 后出现“package_cloud: command not found”
- python - 附加到列表后立即删除密钥
- django - ValueError:无法分配“2”:“Model.field”必须是“Model”实例
- woocommerce - Woccommerce - 获取用户数据到 mailchimp
- python - 名称“编码”未定义
- python - TemplateView.get_template_names() 和 TemplateView.template_name 返回不同的模板
- android - ContentScale.FillWidth 不工作 Jetpack Compose