Views and WPF toolkit datagrid

Topics: Prism v2 - WPF 3.5
May 11, 2010 at 9:01 AM

Hi,

I am building the application using the Compositie Guidance October 2009 version.

Below is the XMAL for the datagrid in a view of a module which is in a region on the left hand of the shell window.

<Grid x:Name="mainGrid" Height="Auto" Width="Auto" RenderTransformOrigin="0.5,0.5">
  <Grid.RenderTransform>
   <TransformGroup>
    <ScaleTransform/>
    <SkewTransform/>
    <RotateTransform/>
    <TranslateTransform/>
   </TransformGroup>
  </Grid.RenderTransform>
  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="*" />
   <ColumnDefinition Width="10" />
   <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
   <RowDefinition Height="Auto" />
   <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <toolkit:DataGrid x:Name="dgResults" Grid.Row="0" Grid.Column="0" AutoGenerateColumns="True" VerticalAlignment="Top" HeadersVisibility="None" ItemsSource="{Binding Path=.}" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" Visibility="Hidden" LoadingRow="dg_LoadingRow" Width="auto" HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Left" FrozenColumnCount="1" IsReadOnly="True"/>
  </Grid>

The code in the code-behind file used to populate the datagrid when the data is passed by presenter is

public List<DataTable> Model
  {
   get
   {
    return this.mainGrid.DataContext as List<DataTable>;
   }
   set
   {     
    dgResults.DataContext = value[0];    
    numberofcpv = value[0].Columns.Count - 1;
    dgResults.Columns[0].CellStyle = (Style)FindResource("DatagridCellStyle");    
    dgResults.Visibility = Visibility.Visible;
   }
  } 

The code below is the event handler used to format the data in each row as transposed data now have the same format for each row.

private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
  {
   DataRowView item = e.Row.Item as DataRowView;
   DataRow row;
   DataGrid dg = (sender) as DataGrid;

   if (e.Row.GetIndex() == 0)
   {
    DataGridRow dgr = e.Row;

    e.Row.Background = new SolidColorBrush(Colors.Gray);
    dgr.FontWeight = FontWeights.Bold;
    if (item != null)
    {
     row = item.Row;
     row["0"] = "";

    }
   }
   else if (e.Row.GetIndex() <= 8)
   {
    if (dg == dgResults)
    {
     for (int i = 1; i <= numberofcpv; i++)
     {
      if (item != null)
      {
       row = item.Row;
       row[i] = string.Format("{0:c0}", Convert.ToDecimal(row[i]));
      }
     }
    }
    }
   else if (9 <= e.Row.GetIndex() && e.Row.GetIndex() <= 12)
   {
    if (dg == dgResults)
    {
     for (int i = 1; i <= numberofcpv; i++)
     {
      if (item != null)
      {
       row = item.Row;
       row[i] = string.Format("{0:0.00%}", Convert.ToDecimal(row[i]));
      }
     }
    }

   }
   else if (9 <= e.Row.GetIndex() && e.Row.GetIndex() <= 13)
   {
    if (dg == dgResults)
    {
     for (int i = 1; i <= numberofcpv; i++)
     {
      if (item != null)
      {
       row = item.Row;
       row[i] = Convert.ToDecimal(row[i]).ToString("N2");
      }
     }
    }
   }
   else
   {
    if (dg == dgResults)
    {
     for (int i = 1; i <= numberofcpv; i++)
     {
      if (item != null)
      {
       row = item.Row;
       row[i] = string.Format("{0:c0}", Convert.ToDecimal(row[i]));
      }
     }
    }
    }
  }

On the right hand side region of the shell window, it is another view from the same module. This view cotains some comboboxes for the selection of search criteria. These comboboxs' default value are set to index 0 when the window is loaded.

When I choose the different items in the comboboxes and click the submit button, the data are loaded and displayed correctly in the grid.

However, when I select a row in the datagrid by clicking on it, while it should have no effect on all the comboboexs,  the currently selected items in them are all changed back to the default items same as the when the window is just loaded.  e.g. if comboboxes has items A,B,c and A is the default selected item when the window is loaded. After I choose B in it and submit the search, when the data is displayed in the datagrid, I click on the row in the grid, the combobox will chagne to A selected in it while it should stay on B.

It has nothing to do with the dg_LoadingRow event handling as well. As even without using it, the problem can still happend.

If I put a combox in the same view with the grid, it seems all right. 

Is there anything else I can use to track the problem?

 

Thanks

May 11, 2010 at 10:54 AM
Edited May 11, 2010 at 10:54 AM

Problems solved. It is something to do with the event bubbling in my code.