简体   繁体   English

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

[英]How can I set a border around a control during runtime in WPF?

I have an Image control on my WPF Form. 我的WPF表单上有一个Image控件。 How can I create a border around it during runtime? 如何在运行时在其周围创建边框?

Here's my XAML code: 这是我的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" />

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? 有什么帮助吗?

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 ... 没有简单的方法可以做到这一点,因为Border是一个容器,因此您必须从其父级中删除该Image ,改为放置Border ,然后将Image放回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;

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. 或者,您可以使用自定义的Adorner在图像周围放置边框。 Good info on Adorners can be found on msdn. 可以在msdn上找到有关装饰器的好信息。

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. 为了满足您的需求,我建议您使用带有自定义ItemContainerStyle的ListBox-一个始终具有边框,但只有在选中该项目时才可见的边框。

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>

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

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