Silverlight:在 DataGrid 绑定后禁用自动第一行选择
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好的解决方法是设置
_isLoading
标志。以下代码是 ViewModel 的正确代码:请查看行
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: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.
不知道为什么会发生这种情况,除非您以某种方式在代码中设置 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.
首先想到的是您需要执行以下操作:
并对
SelectedItem
实施相同的策略。The first thing that comes to mind is that you need to do something like:
And implement that same strategy for
SelectedItem
.我通过将
MoveCurrentTo(null)
放入 PageIndexChanged 事件中解决了这种情况。就像这样:XAML
C#
I solved the situation by putting the
MoveCurrentTo(null)
inside the PageIndexChanged event. Like that:XAML
C#