开发者

Silverlight: disable automatic first-row selection after DataGrid binding

开发者 https://www.devze.com 2023-04-09 14:25 出处:网络
I have a Silverlight DataGrid, a DataPager and a PagedCollectionView. After the PagedCollection view is bound to the DataGrid, the first row of the DataGrid is selected.

I have a Silverlight DataGrid, a DataPager and a PagedCollectionView. After the PagedCollection view is bound to the DataGrid, the first row of the DataGrid is selected.

This behaviour does not happen, if i am using an ObservableCollection. But due to the DataPager, I need to use the PagedCollectionView.

I am using Silverlight 5 RC, and the SL 4 Toolkit.

Example of Usage:

View:

<sdk:DataGrid x:Name="gridSomeItems" ItemsSource="{Binding SomeItems, Mode=TwoWay}" SelectedI开发者_Go百科tem="{Binding SelectedItem, Mode=TwoWay}" CanUserSortColumns="True">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Header="Column1" Width="*" Binding="{Binding Column1}" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>
<sdk:DataPager Source="{Binding Path=ItemsSource, ElementName=gridSomeItems}"/>

ViewModel:

public class SomeViewModel : ViewModelBase
{
    public SomeViewModel(IDataAccess dataAccess)
    {
        _dataAccess = dataAccess;
        _dataAccess.GetSomeItemsCompleted += GetSomeItemsCompleted;
        _dataAccess.GetSomeItems();           
    }

    public PagedCollectionView SomeItems { get; set; }
    public SomeItem SelectedItem { get; set; }

    private void GetSomeItemsCompleted(GetSomeItemsCompletedEventArgs e)
    {
        SomeItems = new PagedCollectionView(e.Result.SomeItems);         
        RaisePropertyChanged("SomeItems");
    }
}


The best workaround is to set a _isLoading flag. The following code would be the correct code for the ViewModel:

public class SomeViewModel : ViewModelBase
{
    private bool _isLoading;

    public SomeViewModel(IDataAccess dataAccess)
    {
        _dataAccess = dataAccess;
        _dataAccess.GetSomeItemsCompleted += GetSomeItemsCompleted;
        _isLoading = true;
        _dataAccess.GetSomeItems();           
    }

    public PagedCollectionView SomeItems { get; set; }
    public SomeItem SelectedItem { get; set; }

    private void GetSomeItemsCompleted(GetSomeItemsCompletedEventArgs e)
    {
        SomeItems = new PagedCollectionView(e.Result.SomeItems);         
        SomeItems.CurrentChanged += CurrentItemChanged;
        RaisePropertyChanged("SomeItems");
        SomeItems.MoveCurrentTo(null);
        _isLoading = false;     
    }

    private void CurrentItemChanged(object sender, System.EventArgs e)
    {
        if (!_isLoading)
        {
            SelectedItem = SomeItems.CurrentItem as SomeItem;
            // Do something more...
        }
    }   
}

Please have a look at the row SomeItems.MoveCurrentTo(null); - This command really sets the CurrentItem of the PagedCollectionView to null (not selected).

It is still a workaround... Would be great if anybody came up with a better solution.


Not sure why that is happening unless you are somehow setting the SelectedItem property in your code somehow.

A simple solution to the problem would be to use a Behavior on the DataGrid that sets the SelectedIndex to -1 when the loaded event fires.


The first thing that comes to mind is that you need to do something like:

private PagedCollectionView someItems;
public PagedCollectionView SomeItems
{
    get { return someItems; }

    set
    {
        if (someItems != value)
        {
            someItems = value;
            RaisePropertyChanged("SomeItems");
        }
    }
}

And implement that same strategy for SelectedItem.


I solved the situation by putting the MoveCurrentTo(null) inside the PageIndexChanged event. Like that:

  1. XAML

    <data:DataPager x:name="pager" PageIndexChanged="dataPagerGroup_PageIndexChanged" />
    
  2. C#

    private void PageIndexChanged(object sender, EventArgs e)
    {
        (this.pager.Source as PagedCollectionView).MoveCurrentTo(null);
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消