使用VisualTreeHelper选择ImageBrush

时间:2014-11-11 07:46:01

标签: c# xaml windows-phone-8 visualtreehelper imagebrush

我正在尝试从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...
}

但在这里,我没有得到所有的画笔。让我,我有十个图像画笔。但我只有两个(大部分时间是第一个和最后一个)图像画笔,它们有源并且还有其他的图像画笔。消息来源是空的。

我的选择有什么不对吗?需要帮助......

0 个答案:

没有答案