0

我正在尝试从 LongListSelector 中选择 ImageBrush 项目。由于 ImageBrushes 在 DataTemplate 中,我使用 VisualTreeHelper 选择它们。

我的示例 xaml 代码:

<phone:LongListSelector Grid.Row="0" Name="AllPhotosListBox"
                                SelectionChanged="AllPhotosListBox_SelectionChanged"
                                ItemsSource="{Binding PhotoItems}" 
                                LayoutMode="Grid" GridCellSize="112, 112">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Border Margin="24,12,-12,0" Grid.Row="2" BorderThickness="1" BorderBrush="Gray" >
                <Grid toolkit:TiltEffect.IsTiltEnabled="True">
                    <Grid.Background>
                        <ImageBrush ImageSource="{Binding ImageUrlLow}" Stretch="UniformToFill" />
                    </Grid.Background>
                </Grid>
            </Border>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

我用于选择 ImageBrushes 的示例 C# 代码是:

for (int i = 0; i < AllPhotosListBox.ItemsSource.Count; i++)
{
    AllPhotosListBox.UpdateLayout();
    AllPhotosListBox.ScrollTo(AllPhotosListBox.ItemsSource[i]);

    LongListSelector longListBoxItem = AllPhotosListBox as LongListSelector;
    longListBoxItem.UpdateLayout();

    var grid = VisualTreeHelper.GetChild(longListBoxItem, 0);
    var grid1 = VisualTreeHelper.GetChild(grid, 0);
    var viewportControl = VisualTreeHelper.GetChild(grid1, 0) as ViewportControl;
    var contentPresenter1 = VisualTreeHelper.GetChild(viewportControl, 0) as ContentPresenter;
    var canvas1 = VisualTreeHelper.GetChild(contentPresenter1, 0);
    var canvas2 = VisualTreeHelper.GetChild(canvas1, 0);
    var ContentPresenter2 = VisualTreeHelper.GetChild(canvas2, 0);
    var border = VisualTreeHelper.GetChild(ContentPresenter2, 0);
    var grid2 = VisualTreeHelper.GetChild(border, 0) as Grid;
    ImageSource imgSource = (grid2.Background as ImageBrush).ImageSource;

    // doing other stuffs here...
}

但在这里我没有得到所有的图像刷。让,我有十个图像刷。但是我只得到了两个(大部分时间是第一个和最后一个)有源的图像刷,而其他人的源是空的。

是不是我的选择有问题。需要帮忙...

4

0 回答 0