首页 > 解决方案 > 单击第一个数据网格行不触发 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。用户需要先选择第二行然后选择第一行才能触发选择第一行

标签: c#wpfwpfdatagrid

解决方案


由于无法看到填充 DataGridView 的代码,我猜您已经选择了第一行,因此单击它不会更改 SelectedIndex。确保在填充 DataGridView 后没有选择行。

禁用当前单元格选择


推荐阅读