如何在 WPF 运行时设置控件周围的边框?

发布于 2024-08-15 15:34:49 字数 542 浏览 1 评论 0原文

我的 WPF 窗体上有一个图像控件。如何在运行时在它周围创建边框?

这是我的 XAML 代码:

<Image Margin="2.5" 
       Grid.Column="1" Grid.Row="0" 
       x:Name="Behemoth" Source="Images/Hero/Behemoth.gif" Stretch="Fill"     
       MouseEnter="HeroMouseEnter" 
       MouseLeave="HeroMouseLeave" 
       MouseDown="HeroMouseClick" />

另外,我想知道如何删除边框。

也许如果我更好地陈述我的问题,就会有更好的解决方案。

我有很多图片,当用户说:“嘿,给我看一下所有图片中的女人吧。”我想要一种方法来突出显示或吸引用户注意我需要他们看到的任何图像。我正在考虑添加一个边框,但对于可以更容易解决的问题来说,这可能是太多的工作。

有什么帮助吗?

I have an Image control on my WPF Form. How can I create a border around it during runtime?

Here's my XAML code:

<Image Margin="2.5" 
       Grid.Column="1" Grid.Row="0" 
       x:Name="Behemoth" Source="Images/Hero/Behemoth.gif" Stretch="Fill"     
       MouseEnter="HeroMouseEnter" 
       MouseLeave="HeroMouseLeave" 
       MouseDown="HeroMouseClick" />

Also, I want to know how to remove the border.

Maybe if I state my problem better there is an even better solution available.

I have many Images, and when a user says: "Hey, just show me the woman out of all the picture." I want a way to sort of highlight or draw the users attention to whatever images I need them to see. I was thinking about adding a border, but maybe that's too much work for something that can be solved easier.

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心房敞 2024-08-22 15:34:49

尽管它在视觉上与边框非常不同,但您可以使用外部发光来表示图像的重要性。然后,您不必更改图像的父级。

或者,您可以使用自定义装饰器在图像周围放置边框。有关装饰器的详细信息可以在 msdn 上找到。

Although it's visually very different from a border, you could use an outter glow to signify the importance of the image. Then, you don't have to change the parent of the image.

Alternatively, you could use a custom Adorner to place a border around the image. Good info on Adorners can be found on msdn.

熟人话多 2024-08-22 15:34:49

没有直接的方法可以做到这一点,因为 Border 是一个容器,因此您必须从其父级中删除 Image,然后将 Border 放入> 相反,并将 Image 放回 Border...

另一种选择是使用模板:

<Window.Resources>
    <ControlTemplate x:Key="imageWithBorder" TargetType="{x:Type Image}">
        <Border BorderBrush="Red" BorderThickness="2">
            <Image Source="{TemplateBinding Source}" />
        </Border>
    </ControlTemplate>
</Window.Resources>

...

   <Image Name="image1" Source="foo.png"/>

当您想要在图像周围放置边框时,只需指定图像模板:

image1.Template = this.FindResource("imageWithBorder") as ControlTemplate;

There's no straightforward way to do it, because the Border is a container, so you would have to remove the Image from its parent, put the Border instead, and put the Image back in the Border...

Another option would be to use templates :

<Window.Resources>
    <ControlTemplate x:Key="imageWithBorder" TargetType="{x:Type Image}">
        <Border BorderBrush="Red" BorderThickness="2">
            <Image Source="{TemplateBinding Source}" />
        </Border>
    </ControlTemplate>
</Window.Resources>

...

   <Image Name="image1" Source="foo.png"/>

When you want to put the border around the image, just assign the template to the image :

image1.Template = this.FindResource("imageWithBorder") as ControlTemplate;
薔薇婲 2024-08-22 15:34:49

对于您所述的需求,我建议您使用带有自定义 ItemContainerStyle 的 ListBox - 始终具有边框但仅在选择项目时才使其可见的列表框。

基本思想如下:

<ListBox ItemsSource="{Binding MyImageObjects}">
  <ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ListBoxItem}">
            <Border x:Name="border">
              <ContentPresenter />
            </Border>
            <ControlTemplate.Triggers>
              <Trigger Property="ListBoxItem.IsSelected" Value="True">
                <Setter ElementName="border" Property="BorderBrush" Value="Blue" />
                <Setter ElementName="border" Property="BorderThickness" Value="2" />
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

For your stated needs, I suggest you use a ListBox with a custom ItemContainerStyle - one that always has a border but only makes it visible if the item is selected.

Here's the basic idea:

<ListBox ItemsSource="{Binding MyImageObjects}">
  <ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ListBoxItem}">
            <Border x:Name="border">
              <ContentPresenter />
            </Border>
            <ControlTemplate.Triggers>
              <Trigger Property="ListBoxItem.IsSelected" Value="True">
                <Setter ElementName="border" Property="BorderBrush" Value="Blue" />
                <Setter ElementName="border" Property="BorderThickness" Value="2" />
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文