积极答复者
论证:GridView生成数据后能否把滚动条推到指定位置

问题
-
我想达到的效果是:如图
为了实现滚动条推动到指定位置,我采用的是bob方法,原地址在这里 http://social.msdn.microsoft.com/Forums/zh-CN/winstoreappzhcn/thread/fc86e310-1a79-4707-a33e-4800f9bdf623:
1,遍历gridview,找到scrollview;
public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); string controlName = child.GetValue(Control.NameProperty) as string; if (controlName == name) { return child as T; } else { T result = FindVisualChildByName<T>(child, name); if (result != null) return result; } } return null; }
2,保存视图的水平位置:
- 已编辑 英明神武可爱 2012年12月2日 7:52
答案
全部回复
-
Hi,
抱歉。
你在哪里调用的ScrollToHorizontalOffset这个方法?这个方法必须要等可视树构建完毕才能调用,否则是没有效果的。
Aaron
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
我在PageLoadedHandler 中调用的
<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="Test.ItemsPage"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
IsTabStop="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test"
xmlns:common="using:Test.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Loaded="PageLoadedHandler"> //页面加载的时候==========================
void ItemsPage::GetData()
{
dataBox->ite->Append(homeData);
this->DataContext = dataBox;
}
void ItemsPage::PageLoadedHandler(Platform::Object^ sender,Windows::UI::Xaml::RoutedEventArgs^ e)
{
GetData(); //数据加载完成
s->ScrollToHorizontalOffset(d); //推动滚动条;
}
- 已编辑 英明神武可爱 2012年12月5日 9:47
-
我在PageLoadedHandler 中调用的
<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="Test.ItemsPage"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
IsTabStop="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test"
xmlns:common="using:Test.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Loaded="PageLoadedHandler"> //页面加载的时候==========================
void ItemsPage::GetData()
{
dataBox->ite->Append(homeData);
this->DataContext = dataBox;
}
void ItemsPage::PageLoadedHandler(Platform::Object^ sender,Windows::UI::Xaml::RoutedEventArgs^ e)
{
GetData(); //数据加载完成
s->ScrollToHorizontalOffset(d); //推动滚动条;
}
Welcome to my sina Weibo:http://t.sina.com.cn/1791733541
-
Hi,
我认为你的代码是没有问题的,我并没有找到有什么问题可能还需要其他的代码比如getdata()里面的代码,如果你觉得贴在论坛上会有隐私问题的话你可以把有问题的工程删掉一些设计隐私的数据之后用邮件发给我 v-axue at microsoft.com
下面是我用来测试的代码,我并没有发现问题,我在上面绑定了一个Textblock来查看HorizontalOffset的值。
XAML:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="100" /> <RowDefinition /> </Grid.RowDefinitions> <TextBlock Name="tb" /> <GridView Grid.Row="1" Name="gv"> <GridView.ItemContainerStyle> <Style TargetType="GridViewItem"> <Setter Property="Height" Value="400" /> <Setter Property="Width" Value="400" /> </Style> </GridView.ItemContainerStyle> <GridView.Items> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> <TextBlock Text="111111" /> </GridView.Items> </GridView> </Grid>
C#:
public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); string controlName = child.GetValue(Control.NameProperty) as string; if (controlName == name) { return child as T; } else { T result = FindVisualChildByName<T>(child, name); if (result != null) return result; } } return null; } private void Page_Loaded_1(object sender, RoutedEventArgs e) { ScrollViewer sv = FindVisualChildByName<ScrollViewer>(this.gv, "ScrollViewer"); this.tb.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("HorizontalOffset"), Source = sv }); sv.ScrollToHorizontalOffset(3); }
Aaron
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Hi,
可以的。
Aaron
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Hi,
能用zip压缩吗?我这里没办法解压这个。
Aaron
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Hi,
我已经接到,谢谢。说实话现在还没找到问题。
Aaron
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Hi,
我和我的同事看过之后也没有找到问题,有可能是信息没有加载完毕的问题,你试试将那个改成异步的做法,c++的不是很熟悉,就是Task,之后用await,让它等待加载的完成。我们认为代码是没有问题的,有可能是这个导致的。
Aaron
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
你好,我直接用VisualTreeHelper::GetChildrenCount(itemGridView);itemGridView是要找的GridView x:Name的值,但是此时这个值为0,为什么?
VisualTreeHelper::GetChildrenCount(itemGridView); OUTPUT("VisualTreeHelper::GetChildrenCount(itemGridView) = %d",VisualTreeHelper::GetChildrenCount(itemGridView));
输出:VisualTreeHelper::GetChildrenCount(itemGridView) = 0;
请帮忙解答
-
你好,我直接用VisualTreeHelper::GetChildrenCount(itemGridView);itemGridView是要找的GridView x:Name的值,但是此时这个值为0,为什么?
VisualTreeHelper::GetChildrenCount(itemGridView); OUTPUT("VisualTreeHelper::GetChildrenCount(itemGridView) = %d",VisualTreeHelper::GetChildrenCount(itemGridView));
输出:VisualTreeHelper::GetChildrenCount(itemGridView) = 0;
请帮忙解答
-
没有隐藏,我GridView x:Name ="itemGridView"我要找里面这个 TextBlock x:Name="CostTxt"
<Grid x:Name="HallPageRaceListGrid" Grid.Column="1" Grid.Row="1" > <GridView x:Name ="itemGridView" Grid.Column="1" Grid.Row="1" IsSwipeEnabled="false" IsItemClickEnabled="True" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource StoreFrontTileStyle_Hall}" ItemsPanel="{StaticResource StoreFrontGridItemsPanelTemplate_Hall}" ScrollViewer.VerticalScrollBarVisibility="Hidden" SelectionMode="None"> <GridView.ItemTemplate> <DataTemplate> <Grid HorizontalAlignment="Left" Width="613" Height="200"> <Grid.Background> <ImageBrush ImageSource="Assets/btn_frame.png"/> </Grid.Background> <Grid.ColumnDefinitions> <ColumnDefinition Width="50*"/> <ColumnDefinition Width="366*"/> <ColumnDefinition Width="157*"/> <ColumnDefinition Width="40*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="25*"/> <RowDefinition Height="66*"/> <RowDefinition Height="57*"/> <RowDefinition Height="57*"/> <RowDefinition Height="25*"/> </Grid.RowDefinitions> <Grid Grid.Column="1" Grid.Row="2" Grid.RowSpan="2"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock TextWrapping="Wrap" Grid.Row="0" Foreground="Black" Style="{StaticResource TitleTextStyle}" VerticalAlignment="Center" Text="{Binding PlayaMountText}" FontSize="{Binding Path=FontSize26,Source={StaticResource customerFontSize}}" HorizontalAlignment="Left" FontFamily="SimHei"/> <TextBlock x:Name="CostTxt" TextWrapping="Wrap" Grid.Row="1" Foreground="Black" Style="{StaticResource TitleTextStyle}" VerticalAlignment="Center" Text="{Binding costText}" FontSize="{Binding Path=FontSize26,Source={StaticResource customerFontSize}}" HorizontalAlignment="Left" FontFamily="SimHei"/> </Grid> <Image Source="{Binding MatchImage}" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" Height="66" Width="415" HorizontalAlignment="Left" VerticalAlignment="Stretch" AutomationProperties.Name="{Binding _Image}"/> <CXComm:ImageButton x:Name="ButtonSignup" Grid.Column="2" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="SignUpClk" BorderThickness="0" IsTabStop="False"> <CXComm:ImageButton.Background> <ImageBrush ImageSource="Assets/btn_signup_n.png"/> </CXComm:ImageButton.Background> <CXComm:ImageButton.PressedBackground> <ImageBrush ImageSource="Assets/btn_signup_d.png"/> </CXComm:ImageButton.PressedBackground> </CXComm:ImageButton> <CXComm:ImageButton x:Name="ButtonAbout" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="RaceAwardInfoClk" BorderThickness="0" IsTabStop="False"> <CXComm:ImageButton.Background> <ImageBrush ImageSource="Assets/btn_about_n.png"/> </CXComm:ImageButton.Background> <CXComm:ImageButton.PressedBackground> <ImageBrush ImageSource="Assets/btn_about_d.png"/> </CXComm:ImageButton.PressedBackground> </CXComm:ImageButton> </Grid> </DataTemplate> </GridView.ItemTemplate> </GridView> </Grid>
-
Hi,
很高兴听到这个消息。欢迎分享解决方法:)。
Aaron
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.