2009-12-27 146 views
0

我有我的WPF窗體上的圖像控件。我如何在運行時創建一個邊框?如何在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" /> 

另外,我想知道如何刪除邊框。

也許如果我更好地說明我的問題,那麼有更好的解決方案可用。

我有很多圖片,當一個用戶說:「嘿,讓我看看這個女人的全部照片。」我想要一種方式來突出顯示或者吸引用戶注意我需要他們看到的任何圖像。我正在考慮添加邊框,但是對於可以更容易解決的問題,這可能太多了。

任何幫助?

回答

1

雖然它在視覺上與邊框非常不同,但您可以使用outter發光來表示圖像的重要性。然後,您不必更改圖像的父級。

或者,您可以使用自定義Adorner在圖像周圍放置邊框。關於Adorners的更多信息可以在msdn上找到。

1

有沒有簡單的方法來做到這一點,因爲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; 
1

爲了您所述的需求,我建議您使用帶有自定義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> 
+0

只是好奇:樣式有一個ListBoxItem的TargetType的,和控件模板也有一個ListBoxItem的TargetType的。 ControlTemplate不是冗餘的嗎?或者這是WPF如何要求它?非常感謝。 – Sabuncu 2014-08-04 11:16:42

相關問題