WPF,DataGrid,单击的项/行不突出显示(蓝色背景)

时间:2015-06-15 17:09:37

标签: c# wpf linq datagrid

这是DataGrid XAML:

<DataGrid CanUserAddRows="False" AutoGenerateColumns="False" Height="501" HorizontalAlignment="Left" Margin="6,6,0,0" Name="dataGridTrades" VerticalAlignment="Top" Width="574" SelectionChanged="dataGridTrades_SelectionChanged">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="ID" Binding="{Binding TradeID}" IsReadOnly="true"/>
                        <DataGridTextColumn Header="Account" Binding="{Binding AccountName}" IsReadOnly="true"/>
                        <DataGridTextColumn Header="Card Name" Binding="{Binding CardName}" IsReadOnly="true"/>
                        <DataGridTextColumn Header="Set" Binding="{Binding SetMark}" IsReadOnly="true"/>
                        <DataGridTextColumn Header="Count" Binding="{Binding TradeCount}" IsReadOnly="true"/>
                        <DataGridTextColumn Header="Trade Date" Binding="{Binding TradeDate}" IsReadOnly="true"/>
                        <DataGridTextColumn Header="Tradepoint Name" Binding="{Binding TradepointName}" IsReadOnly="true"/>
                        <DataGridTextColumn Header="Price" Binding="{Binding Price}"  IsReadOnly="true" Width="*"/>
                    </DataGrid.Columns>
                </DataGrid>

这是一个填充它的查询:

public static List<RowTrade> GetTradeList(DataContext dataContext)
    {
        return (from trade in dataContext.Trades
                join account in dataContext.Accounts on trade.accountID equals account.accountID
                join tradePoint in dataContext.TradePoints on trade.tradePointID equals tradePoint.tradePointID
                join card in dataContext.Cards on trade.cardID equals card.cardID
                join set in dataContext.Sets on card.setID equals set.setID
                select new RowTrade(
                    trade.tradeID,
                    account.accountName,
                    card.cardName,
                    set.setMark,
                    trade.tradeCount,
                    trade.tradeDate.ToString(),
                    tradePoint.tradePointName,
                    (double)trade.cardPrice)).ToList();
    }

我在这里填充它:

private void tabControlOrganizer_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (tabControlOrganizer.SelectedItem != null)
        {
            if (tabControlOrganizer.SelectedItem == tabItemTrades)
            {
                dataGridTrades.ItemsSource = Queries.GetTradeList(dataContext);
            }

当我点击行时,它不会突出显示。我添加了2个事件来检查这种行为:

private void dataGridTrades_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (dataGridTrades.SelectedItem != null)
        {
            listBoxMessage.Items.Add(((RowTrade)dataGridTrades.SelectedItem).TradeID);
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (dataGridTrades.SelectedItem != null)
        {
            listBoxMessage.Items.Add(((RowTrade)dataGridTrades.SelectedItem).TradeID);
        }
    }

当我点击(选择更改)时,我可以在listBox上打印值。所以选择的行不是null。但是在dataGrid松散hightlight之后,它变为null(我点击按钮后无法打印它)。

如何解决?我需要突出显示这个SelectedItem。我有更多的dataGrids,所有行为都正常。

1 个答案:

答案 0 :(得分:1)

以下是有效的解决方案:

private void tabControlOrganizer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   if (tabControlOrganizer.SelectedItem != null)
   {
      if (e.Source is TabControl)
      {
        if (tabItemTrades.IsSelected)
        {
          dataGridTrades.ItemsSource = Queries.GetTradeList(dataContext);
        }
问题是{p> SelectionChangedIn C# WPF, why is my TabControl's SelectionChanged event firing too often?

在空闲时间,我会检查MVVM来解决这个问题,即@Mark W。