Silverlight:在 DataGrid 绑定后禁用自动第一行选择

发布于 2024-12-07 09:01:01 字数 1481 浏览 0 评论 0原文

我有一个 Silverlight DataGrid、一个 DataPager 和一个 PagedCollectionView。 将 PagedCollection 视图绑定到 DataGrid 后,将选择 DataGrid 的第一行。

如果我使用 ObservableCollection,这种行为不会发生。但由于 DataPager,我需要使用 PagedCollectionView

我正在使用 Silverlight 5 RC 和 SL 4 Toolkit。

使用示例:

视图:

<sdk:DataGrid x:Name="gridSomeItems" ItemsSource="{Binding SomeItems, Mode=TwoWay}" SelectedItem="{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");
    }
}

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}" SelectedItem="{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");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云裳 2024-12-14 09:01:01

最好的解决方法是设置 _isLoading 标志。以下代码是 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...
        }
    }   
}

请查看行 SomeItems.MoveCurrentTo(null); - 此命令实际上将 PagedCollectionView 的 CurrentItem 设置为 null(未选择)。

它仍然是一种解决方法......如果有人想出更好的解决方案,那就太好了。

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.

墟烟 2024-12-14 09:01:01

不知道为什么会发生这种情况,除非您以某种方式在代码中设置 SelectedItem 属性。

该问题的一个简单解决方案是在 DataGrid 上使用行为,在加载事件触发时将 SelectedIndex 设置为 -1。

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.

七婞 2024-12-14 09:01:01

首先想到的是您需要执行以下操作:

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

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

并对 SelectedItem 实施相同的策略。

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.

音栖息无 2024-12-14 09:01:01

我通过将 MoveCurrentTo(null) 放入 PageIndexChanged 事件中解决了这种情况。就像这样:

  1. XAML

    
    
  2. C#

    private void PageIndexChanged(对象发送者,EventArgs e)
    {
        (this.pager.Source as PagedCollectionView).MoveCurrentTo(null);
    }
    

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);
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文