使用C#编程 - WPF更改网格面板边框颜色

时间:2015-07-17 15:25:42

标签: wpf xaml wpf-controls gridpanel

我有网格面板,我想在点击按钮

时更改其边框颜色
<Grid Grid.Row="3"  Name="LocationLayoutPanel" VerticalAlignment="Top"  
 Margin="0,51,0,0" Height="65" ShowGridLines="True">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"   />
            <ColumnDefinition Width="9*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions >

 <Border BorderThickness="2,2,2,2" BorderBrush="Red" Grid.ColumnSpan="3"  
   Grid.RowSpan="2"/>

    <Button Content="change border color" Grid.Row="1" Grid.Column="1" 
    Click="chnageBGCOLOR"></Button>
</Grid>

当我点击&#34;更改边框颜色&#34;按钮的网格边框颜色应该改变!

谢谢!

1 个答案:

答案 0 :(得分:1)

<强> XAML:

      <Border BorderThickness="2,2,2,2" Grid.ColumnSpan="3"  
   Grid.RowSpan="2">
            <Border.Style>
                <Style TargetType="Border">
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=Button, Path=IsPressed}" Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                                        To="GreenYellow"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
        </Border>
        <Button Name="Button" Content="change boarder color" Grid.Row="1" Grid.Column="1"></Button>

以编程方式:

将名称添加到边框并设置如下:

BorderName.BorderBrush = new SolidColorBrush(Colors.GreenYellow);