单击第一个数据网格行不触发selectedIndex更改

时间:2018-08-03 18:37:58

标签: c# wpf wpfdatagrid

我有一个数据网格:

<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。用户需要先选择第二行,然后再选择第一行,以触发选择第一行

1 个答案:

答案 0 :(得分:-1)

在无法看到用于填充DataGridView的代码的情况下,我想您已经选择了第一行,因此单击它不会更改SelectedIndex。确保在填充DataGridView之后没有选择任何行。

Disable current cell selection