0

我有这个代码

 <Grid Width="160" x:Name="grd">
                        <Grid.Background>
                            <ImageBrush x:Name="img"  ImageSource="Assets/Icons/tab-inactive.png" />
                        </Grid.Background>
                        <TextBlock x:Name="txtacticve" Text="Rating" Tap="TextBlock_Tap_1" />
                    </Grid>

在点击文本块时,将调用点击事件。如何在后面的代码中获取此点击事件中图像源的完整路径?我真的迷失了如何在我的代码隐藏事件中获取图像源。

4

2 回答 2

3

一种解决方案可能是以下一种:

在您的 XAML 代码中,您可以将您TextBlock.Tag的属性绑定到Grid.Background您需要的属性。Grid如果您不是第一个父级(无需使用递归 C# 代码找到所需的父级),则在 XAML 中执行此操作而不是后面的代码可能很有用:

  <Grid Width="160" x:Name="grd">
        <Grid.Background>
            <ImageBrush x:Name="img"  ImageSource="Assets/Icons/tab-inactive.png" />
        </Grid.Background>
        <TextBlock x:Name="txtacticve" Text="Rating" Tap="TextBlock_Tap_1" Tag="{Binding ElementName=grd, Path=Background}" />
    </Grid>

然后在你后面的代码中,你只需要以TextBlock.Tag这种方式转换和使用属性:

 private void TextBlock_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
 {
    var textBlock = sender as TextBlock;
    if (textBlock != null)
    {
        var test = ((BitmapImage)((ImageBrush)textBlock.Tag).ImageSource).UriSource.OriginalString;
    }
 }
于 2013-10-26T12:51:34.973 回答
0

尝试这个。

private void TextBlock_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
    var grid = (Grid)txtacticve.Parent;
    var img = grid.Background;
    var path = ((BitmapImage)(((ImageBrush)(img)).ImageSource)).UriSource.AbsolutePath;
}
于 2013-10-26T12:21:09.563 回答