2

在我的 WPF DataGrid 中,我在对行进行排序时遇到问题。

我正在从后面的代码中设置新样式,而 datagrid 加载了事件名称。

但是,当我单击标题进行排序时,我的 DataGrid 样式更改为默认值。

在此处输入图像描述

我的 DataGrid 代码:-

  <DataGrid x:Name="dtstandardview" BorderThickness="0" Height="429"  Width="688" BorderBrush="Aqua" MouseLeftButtonDown="dtstandardview_MouseRightButtonUp_1"
                     GridLinesVisibility="None" MouseRightButtonUp="dtstandardview_MouseRightButtonUp_1" 
                     VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False" IsReadOnly="True" 
                      CanUserDeleteRows="False"  AlternationCount="2" CanUserResizeRows="False" Sorting="dtstandardview_Sorting_1"
                     Background="#DCDCDC" HeadersVisibility="Column" CanUserResizeColumns="False"
                      RowHeight="27" SelectionUnit="FullRow" CanUserAddRows="False" MinRowHeight="27" LoadingRow="dtstandardview_LoadingRow" LoadingRowDetails="dtstandardview_LoadingRowDetails" Loaded="dtstandardview_Loaded" Initialized="dtstandardview_Initialized" CellEditEnding="dtstandardview_CellEditEnding" AutoGeneratingColumn="dtstandardview_AutoGeneratingColumn" UnloadingRow="dtstandardview_UnloadingRow" UnloadingRowDetails="dtstandardview_UnloadingRowDetails"    >

                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Margin" Value="10,5" />
                        <Setter Property="VerticalContentAlignment" Value="Bottom"/>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Setter Property="FontSize" Value="12"/>
                        <Setter Property="FontFamily" Value="Arial"/>
                        <Setter Property="Foreground" Value="#404040"/>
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter Property="DataContext">
                            <Setter.Value>
                                <TextBlock Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}">
                                    <TextBlock.Effect>
                                        <DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/>
                                    </TextBlock.Effect>
                                </TextBlock>
                            </Setter.Value>
                        </Setter>

                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.RowBackground >
                    <ImageBrush ImageSource="/ClientApplication;component/Images/second_row_bg.png"/>
                </DataGrid.RowBackground>
                <DataGrid.AlternatingRowBackground>
                    <ImageBrush ImageSource="/ClientApplication;component/Images/bonus_progress_bg.png"/>
                </DataGrid.AlternatingRowBackground>



                <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="VerticalContentAlignment" Value="Center" />
                        <Setter Property="ContentTemplate" >
                            <Setter.Value>
                                <DataTemplate>
                                    <TextBlock Foreground="#404040" FontWeight="Bold" Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}" TextOptions.TextFormattingMode="Display">
                                        <TextBlock.Effect>
                                            <DropShadowEffect BlurRadius="0" Color="#FFFFFF" Direction="-90" Opacity="0.40" ShadowDepth="1"  RenderOptions.ClearTypeHint="Auto" />
                                        </TextBlock.Effect>
                                    </TextBlock>

                                </DataTemplate>
                            </Setter.Value>
                        </Setter>

                        <Setter Property="Background">
                            <Setter.Value>
                                <ImageBrush ImageSource="/ClientApplication;component/Images/table_bg_header.png"/>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush">
                            <Setter.Value>
                                <ImageBrush   ImageSource="/ClientApplication;component/Images/titel_bg.png"/>
                            </Setter.Value>
                        </Setter>

                        <Setter Property="Foreground" Value="#404040" />
                        <Setter Property="BorderThickness" Value="0, 0, 1, 0"/>
                        <Setter Property="Height" Value="26" />
                        <Setter Property="FontSize" Value="14"/>
                        <Setter Property="FontFamily" Value="Arial"/>







                    </Style>
                </DataGrid.ColumnHeaderStyle>



                <DataGrid.Columns>





                    <DataGridTextColumn  Width="125" Header="Table" Binding="{Binding Path=Table}"> 
                    </DataGridTextColumn>





                    <DataGridTextColumn  Width="101"   Header="Stakes" Binding="{Binding Path=Stakes}"  />
                    <DataGridTextColumn  Width="95" Header="Game" Binding="{Binding Path=Game}" />
                    <DataGridTemplateColumn Header="Type" Width="86">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate x:Name="typedatatempl">
                                <StackPanel Orientation="Horizontal" Name="stackpaneltype">
                                    <TextBlock Name="typedate"  Text="{Binding Path=Type}" Margin="2,0,0,2" />

                                    <TextBlock Name="txtblocknumber" Text="{Binding Path=number}" Margin="2, 0, 0, 2" Background="Gray" TextAlignment="Center" FontFamily="Arial" FontWeight="Bold" FontSize="10" Foreground="White" Height="13" Width="13" VerticalAlignment="Center" HorizontalAlignment="Center" />
                                    <TextBlock Name="txtblockalpha"  Text="{Binding Path=alphabate}" Margin="5,0,0,2" Background="Yellow" FontFamily="Arial" TextAlignment="Center" FontWeight="Bold" FontSize="10" Foreground="White" Height="13" Width="13" VerticalAlignment="Center"  HorizontalAlignment="Center"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn  Width="83" Header="Players" Binding="{Binding Path=Players}"  />
                    <DataGridTextColumn  Width="117" Header="Average Pot" Binding="{Binding Path=Average}"   />
                    <DataGridTextColumn  Width="63" Header="H/Hr" Binding="{Binding Path=hhr}"  />
                </DataGrid.Columns>



            </DataGrid>

数据网格.xaml.cs

 int i = e.Row.GetIndex();
            // SetDatGrid();
            // to get the all the row one by one.
            DataGridRow objrow = new DataGridRow();
            DataGridCell objcell = new DataGridCell();
         //   objrow = ExtensionMethods.GetRow(dtstandardview, i);
            objcell = ExtensionMethods.GetCell(dtstandardview, e.Row, 3);

            ContentPresenter objcontent = new ContentPresenter();
            if (objcell != null)
            {
                objcontent = (ContentPresenter)objcell.Content;

                GameClassTemp objGameClassTemp = new GameClassTemp();
                objGameClassTemp = (GameClassTemp)objcontent.Content;

                ContentPresenter cp = dtstandardview.ItemContainerGenerator.ContainerFromItem(objcell) as ContentPresenter;
                StackPanel objstackpanel = ExtensionMethods.FindVisualChild<StackPanel>((ContentPresenter)objcell.Content);
                TextBlock objtypedate = (TextBlock)objstackpanel.FindName("typedate");
                TextBlock objtxtblocknumber = (TextBlock)objstackpanel.FindName("txtblocknumber");
                TextBlock objtxtblockalpha = (TextBlock)objstackpanel.FindName("txtblockalpha");
                if (objGameClassTemp != null)
                {
                    if (objGameClassTemp.alphabate != null && objGameClassTemp.alphabate.Trim().Length > 0)
                    {

                        var bc = new BrushConverter();
                        switch (objGameClassTemp.number)
                        {
                            case "1":
                                objtxtblockalpha.Background = Brushes.Green;
                                break;
                            case "2":
                                objtxtblockalpha.Background = Brushes.Yellow;
                                break;
                            case "3":
                                objtxtblockalpha.Background = Brushes.Red;
                                break;
                            case "4":

                                objtxtblockalpha.Background = (Brush)bc.ConvertFrom("#85ab33");
                                break;
                            case "5":
                                //objtxtblockalpha.Background = Brushes.Pink;

                                objtxtblockalpha.Background = (Brush)bc.ConvertFrom("#f9572a");
                                break;

                            case "6":

                                objtxtblockalpha.Background = (Brush)bc.ConvertFrom("#e0d921");
                                break;

                            default:
                                objtxtblockalpha.Background = Brushes.YellowGreen;
                                break;
                        }

                    }
                    else
                    {
                        objtxtblockalpha.Background = Brushes.Transparent;
                    }
                }
            }
4

1 回答 1

0

您需要使用 LoadingDataRow 事件

dataGrid.LoadingRow += dataGrid_LoadingRow;
void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
   var dataContext = e.Row.DataContext;
}
于 2013-02-09T08:42:47.217 回答