访问列表框中的datatemplate元素?

时间:2013-03-22 19:46:41

标签: c# xaml windows-8

我有一个由列表框中的datatemplate生成的图像控件。如何通过代码访问此图像?我试过this但是因为我正在使用metro应用,因此无法设置IsSynchronizedWithCurrentItem。

我拥有的xaml是:

    <ListBox x:Name="options_stack" HorizontalAlignment="Left" Margin="198,569,0,33" Width="603" Background="#FF603C74" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollMode="Auto" >
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image x:Name="options_image" Source = "{Binding}" Stretch="Fill" Width="166" MaxHeight="156" MaxWidth="166" MinHeight="156" MinWidth="166" Tapped="apply_image_tapped" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

2 个答案:

答案 0 :(得分:0)

这取决于您通过访问图像尝试实现的目标。您可以将其属性绑定到视图模型,并使用视图模型对其进行控制。您还可以将Image放在UserControl中并处理您想要的任何内容。您可以使用附加行为,也可以使用VisualTreeHelper扫描ListBox的可视树(尽管这可能很慢)。

答案 1 :(得分:0)

我想获得拍摄图像的路径,所以这就是我所做的:

    Image tappedImage = new Image();
    tappedImage = e.OriginalSource as Image;
    textbox.Text = tappedImage.DataContext.ToString();