如何在工具提示中获取图像属性

时间:2018-04-10 11:01:06

标签: wpf image tooltip

我有一项任务是在Tooltip中获取图像类型,尺寸和大小。我尝试使用此代码。我有Image url,无法在ToolTip中获取Image属性..

 <Image Source="{Binding Path=UriSource}" Stretch="Fill" Width="100" Height="120">
    <Image.ToolTip>
         <ToolTip Content="{Binding}"/>
    </Image.ToolTip>
</Image>

如何在工具提示WPF中获取图像尺寸?

2 个答案:

答案 0 :(得分:3)

由于您的Binding源对象似乎是一个BitmapSource,您可以直接绑定到其属性,例如它的PixelWidth和PixelHeight:

<Image Source="{Binding}" Width="100" Height="120">
    <Image.ToolTip>
        <ToolTip Content="{Binding}">
            <ToolTip.ContentTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding PixelWidth, StringFormat=Width: {0}}"/>
                        <TextBlock Text="{Binding PixelHeight, StringFormat=Height: {0}}"/>
                    </StackPanel>
                </DataTemplate>
            </ToolTip.ContentTemplate>
        </ToolTip>
    </Image.ToolTip>
</Image>

或更短:

<Image Source="{Binding}" Width="100" Height="120">
    <Image.ToolTip>
        <StackPanel>
            <TextBlock Text="{Binding PixelWidth, StringFormat=Width: {0}}"/>
            <TextBlock Text="{Binding PixelHeight, StringFormat=Height: {0}}"/>
        </StackPanel>
    </Image.ToolTip>
</Image>

如果绑定源不是BitmapSource,但是例如只是图像文件Uri或路径字符串,您可以绑定到Image的Source属性中的(自动创建的)BitmapSource对象,如下所示: / p>

<Image Source="{Binding}" Width="160" Height="120">
    <Image.ToolTip>
        <ToolTip DataContext="{Binding PlacementTarget.Source,
                                       RelativeSource={RelativeSource Self}}">
            <StackPanel>
                <TextBlock Text="{Binding PixelWidth, StringFormat=Width: {0}}"/>
                <TextBlock Text="{Binding PixelHeight, StringFormat=Height: {0}}"/>
            </StackPanel>
        </ToolTip>
    </Image.ToolTip>
</Image>

答案 1 :(得分:1)

如果要获取父控件的属性,则必须设置DataContext的{​​{1}}:

ToolTip

monkey

相关问题