2009-11-25 48 views
30

我創建了一個靜態資源,用於定義xaml中特定項目的邊框,但我無法找到爲每個邊都定義唯一顏色的好方法!爲每個角落使用不同刷子顏色的邊框

XAML:

<Border Style="{StaticResource SidePanelBorder}"> 
     <!-- rest of the xaml --> 
</Border> 

靜態資源:

<Style x:Key="SidePanelBorder"> 
    <Setter Property="Control.BorderBrush" Value="#FF363636" /> 
    <Setter Property="Control.BorderThickness" Value="1" /> 
</Style> 

但我想定義一個顏色的邊界兩側,並最終也不同的邊框厚度。

有沒有這樣做的好技術?

+0

我想創建一個使用邊框的插入效果 – 2009-11-25 13:14:05

回答

49

似乎很哈克,但你可以邊框中定義的邊界,並只有1側的厚度。例如,

<Border BorderThickness="0,0,0,10" BorderBrush="Green"> 
    <Border BorderThickness="0,0,10,0" BorderBrush="Blue"> 
     <Grid> 
      <Button>Hello</Button> 
     </Grid> 
    </Border> 
</Border> 

會在底部給出綠色邊框,在右邊給出藍色邊框。儘管Xaml並不是最漂亮的一塊。

+0

這正是我發現的最好的解決方案。不應該有必要爲這樣簡單的事情引入2個邊界,但我想我必須去尋求解決方案!謝謝 – 2009-11-26 08:19:15

+5

這將工作與圓角? – eriksmith200 2010-10-12 13:48:38

2

有沒有簡單的方法來做到這一點,而無需編寫自己的控制還是繼承邊框

+5

嗯,這是一個恥辱!應該像HTML和CSS那樣容易,你有邊框,邊框等等! – 2009-11-25 13:17:20

9

你可以擁有一個DockPanel,並且可以將4個邊界放入其中,每個邊界都停靠在不同的一側。 像:

<DockPanel LastChildFill="true"> 
    <Border DockPanel.Dock="Left" Background="Red"/> 
    <Border DockPanel.Dock="Top" Background ="Blue"/> 
    <Border DockPanel.Dock="Right" Background ="Yellow"/> 
    <Border DockPanel.Dock="Bottom" Background ="Green"/> 
    <Grid> 
    ...........your control here 
    </Grid> 
</DockPanel> 
+2

有趣的做法。 – Tower 2012-04-22 14:18:22

4

如果您使用網格,你可以有邊框的彼此疊加來達到相同的效果。只需設置要顯示和具有邊框顏色的邊框厚度其它邊框厚度爲0

<UserControl.Resources> 
     <Style x:Key="GreenBorder" TargetType="Border"> 
      <Setter Property="BorderBrush" Value="Green" /> 
      <Setter Property="BorderThickness" Value="1,1,1,0" />   
     </Style> 
     <Style x:Key="RedBorder" TargetType="Border"> 
      <Setter Property="BorderBrush" Value="Red" /> 
      <Setter Property="BorderThickness" Value="0,0,0,1" /> 
     </Style> 
    </UserControl.Resources> 

    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <Border Grid.Column="0" Grid.Row="0" Style="{StaticResource GreenBorder}"> 
      <!-- Content goes here --> 
     </Border> 
     <Border Grid.Column="0" Grid.Row="0" Style="{StaticResource RedBorder}"> 
     </Border> 
    </Grid> 

這會給一個綠色邊框的左側,頂部和右側邊框,而是一個紅色邊框的Grid單元格的底部邊框。

20

使用一個邊境和VisualBrush,允許設置邊框的CornerRadius和了borderThickness,另一種解決方案:

<Border BorderThickness="10" CornerRadius="10" HorizontalAlignment="Right" Height="150" VerticalAlignment="Bottom" Width="150" Margin="0,0,92.666,42.667"> 
    <Border.BorderBrush> 
     <VisualBrush> 
      <VisualBrush.Visual> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition/> 
         <ColumnDefinition/> 
        </Grid.ColumnDefinitions> 

        <Path x:Name="ColoredBorderLeft" Data="M0,0 L0,0 1,0.5 L1,0.5 0,1" Fill="Blue" Stretch="Fill" Grid.RowSpan="2"/> 
        <Path x:Name="ColoredBorderRight" Data="M1,0 L1,0 0,0.5 L0,0.5 1,1" Fill="Red" Stretch="Fill" Grid.Column="1" Grid.RowSpan="2"/> 
        <Path x:Name="ColoredBorderTop" Data="M0,0 L0,0 0.5,1 L0.5,1 1,0" Fill="Green" Stretch="Fill" Grid.ColumnSpan="2"/> 
        <Path x:Name="ColoredBorderBottom" Data="M0,1 L0,1 0.5,0 L0.5,0 1,1" Fill="Yellow" Stretch="Fill" Grid.Row="1" Grid.ColumnSpan="2"/> 
       </Grid> 
      </VisualBrush.Visual> 
     </VisualBrush> 
    </Border.BorderBrush> 
</Border> 
  • 需要的電網,以防止三角形路徑的提示,以「通過推動」入邊界。
  • Path.Name's可用於DataBinding或設置代碼後面的顏色。
+0

非常好,並與圓角工作 – 2014-10-08 16:17:59