【问题标题】:C# Change backgroundcolor specific rowC#更改背景颜色特定行
【发布时间】:2013-03-12 08:20:22
【问题描述】:

我从 Grid App (XAML) 模板(C# Windows Store)创建了一个新项目。 到目前为止,我没有更改模板中的任何内容,但我想更改网格中特定行的背景颜色。

<!--
    This grid acts as a root panel for the page that defines two rows:
    * Row 0 contains the back button and page title
    * Row 1 contains the rest of the page layout
-->
<Grid Style="{StaticResource LayoutRootStyle}">
    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

我想更改第 0 行(包含页面标题)的背景颜色。 有任何想法吗??提前致谢!

这一行包括:

    <!-- Back button and page title -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
        <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Grid.Column="1" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}"/>
    </Grid>

【问题讨论】:

    标签: c# wpf silverlight xaml


    【解决方案1】:

    您不能在 Grid.Row 本身上设置背景颜色,而是在占据该行的任何内容上设置 Background 属性。

    例如

    <Grid Style="{StaticResource LayoutRootStyle}">
      <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <Grid Background="Red" Grid.Row="0">
        <TextBlock>Test</TextBlock>
      </Grid>
    </Grid>
    

    编辑: Silverlight 更新; TextBlock 没有背景,因此您必须将控件放在另一个具有背景的边框或网格容器中。代码已更新以反映这一点。

    【讨论】:

    • 不幸的是,文本块项目不包含背景属性。我已经编辑了我的帖子以显示该行中有哪些项目。
    • 啊,对不起,我没有看到你帖子上的silverlight标签,WPF TextBlock有背景。
    • 但是我不想只改变按钮的背景,它必须是整行。
    • 我已经相应地更新了我的答案,您必须将控件包装在边框或网格中(确实具有背景属性的东西)
    • 成功了,谢谢!
    【解决方案2】:

    在你需要的地方插入边框怎么样

    <Grid Style="{StaticResource LayoutRootStyle}">
      <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
        <Border Background="Red" Grid.ColumnSpan="1"></Border>
        <TextBlock>Test</TextBlock>
        <Border Background="blue" Grid.Row="1" Grid.ColumnSpan="1"></Border>
        <TextBlock Grid.Row="1">Test2</TextBlock>
     </Grid>
    

    请注意,如果包含更多列,您可以指定列跨度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 1970-01-01
      • 2017-10-17
      • 2021-12-27
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多