简体   繁体   English

WPF图片旁边的文字+鼠标悬停

[英]WPF image next to text + mouse over

I'm a noobie when it comes to WPF xaml so i'm hoping my question is so easy it can be answered in one line. 关于WPF xaml,我是个菜鸟,所以我希望我的问题如此简单,可以在一行中回答。

I'm looking for the best way to display an icon next to a block of text. 我正在寻找在文本块旁边显示图标的最佳方法。

When a user hovers over the block of text or the icon i want to change the icon to another one. 当用户将鼠标悬停在文本或图标的块上时,我想将图标更改为另一图标。

Also, is it best practice to create one image with all my icons inside?? 另外,最好的做法是创建一个包含我所有图标的图像吗? and move the background to the correct area? 并将背景移到正确的区域?

One approach might be to bind the visibility of the image to the IsMouseOver property of the TextBlock, like this: 一种方法可能是将图像的可见性绑定到TextBlock的IsMouseOver属性,如下所示:

<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis" />
    </StackPanel.Resources>

    <Image 
        Source="foo.jpg" 
        Margin="0 0 5 0" 
        Visibility="{Binding IsMouseOver,ElementName=text,Converter={StaticResource BoolToVis}"
        />

    <TextBlock x:Name="text" Text="Mouse over me to show the image!" />
</StackPanel>

That's untested, but it should be sound. 这未经测试,但应该是正确的。 Let me know if it works for you. 请让我知道这对你有没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM