边框样式只能应用一次

时间:2019-11-09 07:58:11

标签: c# wpf grid styles border

进入网格,我尝试在所有TextBlock周围应用带有样式的边框。 我在XAML代码上没有问题,但是在外观上,当我在网格中的TextBlock周围以相同的样式应用第二个Border(Col:0行:1)时,这一个在网格中移动(Col:0行:0)。 / p>

在我的窗口中。资源:

        <Style x:Key="BorderTextBlockStyle" TargetType="{x:Type Border}">
           <Setter Property="BorderThickness" Value="1"/>
           <Setter Property="BorderBrush" Value="Black"/>
           <Setter Property="CornerRadius" Value="5"/>
           <Setter Property="Grid.Column" Value="{Binding}"/>
           <Setter Property="Grid.Row" Value="{Binding}"/>
        </Style>

在我的窗口中:

<Grid VerticalAlignment="Top">            
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
            <Border Style="{StaticResource BorderTextBlockStyle}">
                <TextBlock Grid.Column="0" Grid.Row="0" Text="Firstname" Style="{StaticResource GridTitleStyle}"/>
            </Border>
            <Border Style="{StaticResource BorderTextBlockStyle}">
                <TextBlock Grid.Column="1" Grid.Row="0" Text="Lastname" Style="{StaticResource GridTitleStyle}"/>
            </Border>
        </Grid>

视觉确定: This picture shows the first border with style work
视觉失败: this picture shows the second border with style fail

为什么?
我怎样才能在我的xaml边框样式中简单定义多次使用?

1 个答案:

答案 0 :(得分:1)

您应该为Grid.Row而非Grid.Column设置BorderTextBlock

     <Border Grid.Column="0" Grid.Row="0" Style="{StaticResource BorderTextBlockStyle}">
            <TextBlock Text="Firstname" Style="{StaticResource GridTitleStyle}"/>
     </Border>
     <Border Grid.Column="1" Grid.Row="0" Style="{StaticResource BorderTextBlockStyle}">
            <TextBlock Text="Lastname" Style="{StaticResource GridTitleStyle}"/>
     </Border>

并在“资源”中删除这些行

    <Setter Property="Grid.Column" Value="{Binding}"/>
    <Setter Property="Grid.Row" Value="{Binding}"/>