在wpf datagrid中刷新数据后,保持选定的行突出显示

时间:2016-09-26 12:45:50

标签: c# wpf datagrid

我目前正在使用rightFax应用程序,我希望即使在数据刷新后也会保持所选行的突出显示,网格每分钟刷新一次数据,何时会丢失突出显示的行。有什么方法可以保持突出显示的行?

请帮助我谢谢

 <Grid>
    <Toolkit:BusyIndicator IsBusy="{Binding IsBusy}">
        <Grid>
            <DataGrid x:Name="dg_Faxes"  AutoGenerateColumns="False" MinHeight="100" MinWidth="100" Margin="5" SelectedItem="{Binding SelectedFax}"
              SelectionUnit="FullRow"  CanUserAddRows="False" HorizontalAlignment="Stretch" ItemsSource="{Binding Faxes}"
              GridLinesVisibility="Horizontal" IsSynchronizedWithCurrentItem="True" Sorting="dg_Faxes_Sorting" 
              HeadersVisibility="Column" SelectedValuePath="{Binding Path=Handle}">
            <DataGrid.ContextMenu>
                <ContextMenu IsOpen="{Binding IsContextOpened}">
                    <MenuItem Header="View Fax Image" Command="{Binding ViewFaxCommand}"></MenuItem>
                    <MenuItem Header="Submit For Process" Command="{Binding SubmitFaxCommand}"></MenuItem>
                    <!--<MenuItem Header="Clear Comments" Command="{Binding ClearCommentsCommand}"></MenuItem>-->
                </ContextMenu>
            </DataGrid.ContextMenu>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Date/Time" MinWidth="100" CanUserSort="True" SortMemberPath="FaxRecordDateTime" 
                                Binding="{Binding Path=FaxRecordDateTime, StringFormat={}\{0:MMM dd\,\ yyyy hh:mm:ss tt\}}" IsReadOnly="True" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="VerticalAlignment" Value="Center"></Setter>
                            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <!--<DataGridTextColumn Binding="{Binding Path=FaxFilename}" Header="Name" CanUserSort="True" SortMemberPath="FaxFilename">
                </DataGridTextColumn>-->
                <DataGridTextColumn Header="Pages" CanUserSort="True" SortMemberPath="TotalPages"
                                Binding="{Binding Path=TotalPages}" IsReadOnly="True">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="VerticalAlignment" Value="Center"></Setter>
                            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTemplateColumn CanUserSort="True" SortMemberPath="IsPrinted" IsReadOnly="True" >
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <Image Width="25" x:Name="IMG">
                                <Image.Source>
                                    <BitmapImage DecodePixelWidth="25" 
                                                 UriSource="../Icons/printer.png"></BitmapImage>
                                </Image.Source>
                                <Image.ToolTip>
                                    <ToolTip>
                                        <StackPanel>
                                            <TextBlock FontWeight="Bold">Is Printed Column</TextBlock>
                                            <TextBlock>Indicates Where A Document Has Been Printed</TextBlock>
                                        </StackPanel>
                                    </ToolTip>
                                </Image.ToolTip>
                            </Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Width="20" x:Name="IMG">
                                <Image.Source>
                                    <BitmapImage DecodePixelWidth="20" 
                                                 UriSource="../Icons/printer.png"></BitmapImage>
                                </Image.Source>
                            </Image>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=IsPrinted}" Value="False">
                                    <Setter Property="Visibility" Value="Hidden" TargetName="IMG"></Setter>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn CanUserSort="True" SortMemberPath="IsViewed" IsReadOnly="True">                        
                    <DataGridTemplateColumn.HeaderTemplate>                            
                        <DataTemplate>
                            <Image Width="25" x:Name="IMG">
                                <Image.Source>
                                    <BitmapImage DecodePixelWidth="25" 
                                                 UriSource="../Icons/eyeIcon.png"></BitmapImage>
                                </Image.Source>
                                <Image.ToolTip>
                                    <ToolTip>
                                        <StackPanel>
                                            <TextBlock FontWeight="Bold">Is Viewed Column</TextBlock>
                                            <TextBlock>Indicates Where A Document Has Been Viewed</TextBlock>
                                        </StackPanel>
                                    </ToolTip>
                                </Image.ToolTip>
                            </Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Width="20" x:Name="IMG">
                                <Image.Source>
                                    <BitmapImage DecodePixelWidth="20" 
                                                 UriSource="../Icons/eyeIcon.png"></BitmapImage>
                                </Image.Source>
                            </Image>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=IsViewed}" Value="False">
                                    <Setter Property="Visibility" Value="Hidden" TargetName="IMG"></Setter>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>                        
                </DataGridTemplateColumn>
                <!--<DataGridTextColumn Header="IsPrinted" CanUserSort="True" SortMemberPath="IsPrinted"
                                Binding="{Binding Path=IsPrinted}" IsReadOnly="True" >
                </DataGridTextColumn>-->
                <!--<DataGridTextColumn Header="IsDeleted" CanUserSort="True" SortMemberPath="IsDeleted"
                                Binding="{Binding Path=IsDeleted}" IsReadOnly="True" >
                </DataGridTextColumn>-->
                <!--<DataGridTextColumn Header="IsApproved" CanUserSort="True" SortMemberPath="IsApproved"
                                Binding="{Binding Path=IsApproved}" IsReadOnly="True" >
                </DataGridTextColumn>-->
                <!--<DataGridTextColumn Header="IsViewed" CanUserSort="True" SortMemberPath="IsViewed"
                                Binding="{Binding Path=IsViewed}" IsReadOnly="True" >
                </DataGridTextColumn>-->
                <DataGridTextColumn Header="Comments" CanUserSort="True" SortMemberPath="UserComments"
                                Binding="{Binding Path=UserComments}" IsReadOnly="True" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="VerticalAlignment" Value="Center"></Setter>
                            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <!--<DataGridTextColumn Header="Status" CanUserSort="True" SortMemberPath="StatusDescription"
                                Binding="{Binding Path=StatusDescription}" IsReadOnly="True" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="VerticalAlignment" Value="Center"></Setter>
                            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>-->
                <DataGridTemplateColumn Header="Status" CanUserSort="True" SortMemberPath="StatusDescription" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="20" x:Name="IMG" Source="../Icons/Red.png"></Image>
                                <TextBlock Text="{Binding Path=StatusDescription}" Margin="5" VerticalAlignment="Center"></TextBlock>
                            </StackPanel>                                
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding Path=StatusDescription}" Value="OK">
                                    <Setter Property="Source" Value="../Icons/Green.png" TargetName="IMG"></Setter>
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
            <Toolkit:ChildWindow Caption="{Binding ChildName}" IsModal="True" Grid.Row="1" HorizontalContentAlignment="Center"
                         WindowState="{Binding ChildState}" MinWidth="300" WindowStartupLocation="Center">
            <Grid>
                <ContentControl
                        prism:RegionManager.RegionName="{x:Static Constants:RegionNames.FaxListChildRegion}"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            HorizontalContentAlignment="Stretch"
                            VerticalContentAlignment="Stretch" />
            </Grid>
            </Toolkit:ChildWindow>
        </Grid>
    </Toolkit:BusyIndicator>
</Grid>

和背后的代码

公共部分类FaxListsView:UserControl,IFaxListsView     {         private ListSortDirection lastSortDirection;         private string lastSortMemberPath;         公共FaxListsView()         {             的InitializeComponent();

    }

    public IViewModel ViewModel
    {
        get
        {
            return (IViewModel)this.DataContext;
        }

        set
        {
            this.DataContext = value;
        }
    }

    private void dg_Faxes_Sorting(object sender, DataGridSortingEventArgs e)
    {
        ((IFaxListsViewModel)this.DataContext).Sorted = true;
        if (e.Column.SortDirection != null && e.Column.SortDirection == ListSortDirection.Ascending)
            ((IFaxListsViewModel)this.DataContext).SortDirection = "Descending";
        else
            ((IFaxListsViewModel)this.DataContext).SortDirection = "Ascending";

        ((IFaxListsViewModel)this.DataContext).SortColumnIndex = e.Column.DisplayIndex;


    }
    public void SortDataGrid(int columnIndex = 0, ListSortDirection sortDirection = ListSortDirection.Ascending)
    {

        lastSortDirection = sortDirection;

        var column = dg_Faxes.Columns[columnIndex];

        dg_Faxes.Items.SortDescriptions.Clear();

        dg_Faxes.Items.SortDescriptions.Add(new SortDescription(column.SortMemberPath, sortDirection));

        foreach (var col in dg_Faxes.Columns)
        {
            col.SortDirection = null;
        }

        column.SortDirection = sortDirection;

        dg_Faxes.Items.Refresh();
    }

0 个答案:

没有答案