1

Here is the 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>

Here is a query to populate it:

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();
    }

Here I populate it:

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

When I click on row, it doesn't hightlight. I added 2 events to check this behavior:

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);
        }
    }

While I'm clicking (selection changed), I can print value on listBox. So selected row is not null. But after dataGrid loose hightlight, it become null (I can't print it after clicking button).

How to fix it? I need to have this SelectedItem to be highlighted. I have couple more dataGrids and all behave normal.

Pochmurnik
  • 780
  • 6
  • 18
  • 35
  • This looks pretty un-MVVM. Are you following MVVM in this app? If not, you're making tons of trouble for yourself, and will find yourself fighting the framework more often than not. – Mark W Jun 15 '15 at 18:09
  • I'm not, this is last tab page in this app, I don't want learn MVVM right now, I prefer to finish it asap move on with other things. But while waiting for solution I will check what MVVM is. And you have right, this looks like fighting. – Pochmurnik Jun 15 '15 at 18:12
  • I mention it because, ive been learning WPF as well, and hanging out in the WPF chat. I took the standard WinForm type approach (which looks like what you did) and CONSTANTLY had problems. Once I switched to MVVM, not only was I able to get help more quickly, but I had less problems in general. – Mark W Jun 15 '15 at 18:14
  • I'm not stubborn but now I cant use MVVM to solve this. – Pochmurnik Jun 15 '15 at 18:21
  • When I move `dataGridTrades.ItemsSource = Queries.GetTradeList(dataContext);` to `public MainWindow(){}` it works. I removed populating from `if (tabControlOrganizer.SelectedItem == tabItemTrades) {}`, Why there is a difference? How to handle tabItem selection changes? I still want populate dataGrid in this IF. – Pochmurnik Jun 15 '15 at 20:32
  • Its going to be hard to get an answer on this pochmurnik. While it is possible, the framework you are using was not designed to be used in this way. People aren't going to know what the issues is (more often than not really) because this isn't how you're supposed to use the framework. Its probably best if you use MVVM for just this tab, at the very least... its not as difficult as you think. http://wpftutorial.net/DataGrid.html I'm not suggesting you rewrite the app, I'm suggesting you take the MVVM approach for this tab.... it will be easier I assure you. – Mark W Jun 15 '15 at 21:12

1 Answers1

2

Here is the solution that works:

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);
        }

SelectionChanged was the problem: In C# WPF, why is my TabControl's SelectionChanged event firing too often?

In free time I will check MVVM to solve this, ty @Mark W.

Community
  • 1
  • 1
Pochmurnik
  • 780
  • 6
  • 18
  • 35