ItemControl背景色 [英] ItemControl background color

查看:71
本文介绍了ItemControl背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个新的Universal Windows Platform应用程序中,我试图设置ItemsControl的背景.但这似乎无能为力.我对VS模板所做的唯一更改是在MainPage.xaml中,现在看起来像这样:

In a fresh Universal Windows Platform app, I am trying to set the Background of an ItemsControl. But it doesn't seem to do anything. The only changes I've made to the VS template are in MainPage.xaml, which now looks like this:

<Page
    x:Class="UWPPlayground.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPPlayground"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" x:Name="Hello">
  <Grid Background="Blue">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*">

      </ColumnDefinition>
      <ColumnDefinition Width="*">

      </ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ItemsControl Grid.Row="0" Grid.Column="0" Width="60" Height="30" Foreground="Wheat" Background="White">
      <TextBlock Text="Hello World!"></TextBlock>
      <TextBlock Text="Can you see this?"></TextBlock>
    </ItemsControl>
    <Grid Grid.Row="0" Grid.Column="1" Background="Purple"></Grid>
    </Grid>
</Page>

结果如下所示. ItemsControl的Foreground属性似乎工作得很好,因为TextBlocks具有小麦色的文本.由于控件的大小很小,因此文本被截断了,正如预期的那样.但是,背景是不可见的.我想念什么?

The result is shown below. The Foreground property of the ItemsControl seems to be working just fine, as the TextBlocks have wheat-colored text. Due to the small size of the control, the text is cut-off, as expected. The Background, however, is not visible. What am I missing?

推荐答案

ItemsControl继承自Control,Control在基类级别定义了许多可视属性,这些可视属性不一定直接影响控件的外观.这些属性通常通过ControlTemplate中的TemplateBindings引用,然后产生所需的外观.模板是否使用这些属性决定了它们是否有任何用途.

ItemsControl inherits from Control, which defines lots of visual properties at the base class level which do not necessarily influence the appearance of the control directly. These properties are usually referenced via TemplateBindings in the ControlTemplate, which then gives rise to the desired appearance. Whether or not the template uses these properties determines whether or not they have any use at all.

您会注意到,更改UserControl的背景也不会执行任何操作(出于上述相同的原因).

You'll notice that changing a UserControl's background also does nothing (for the same reason mentioned above).

非控件类(如Grid,Rectangle,Border(等) do )具有开箱即用的属性,因为这些属性通常用于控件模板中以产生某种外观.

Non-control classes like Grid, Rectangle, Border (etc) do honor such properties out of the box, since these are the elements typically used in the templates of controls to produce a certain appearance.

ItemsControl派生的类(如ListView)之所以使用background属性的原因是因为其模板中的某些根级元素引用了Background属性(通过TemplateBinding). ItemsControl本身没有模板.

The reason why ItemsControl-derived classes (like ListView) do honor the background property is because some root-level element in its template references the Background property (via TemplateBinding). ItemsControl on its own has no template.

我认为Foreground属性起作用的原因是因为它将从父级继承其值. (某些依赖项属性可以像这样继承其值).

I think the reason why the Foreground property works is because it will inherit its value from the parent. (Some dependency properties can inherit their values like this).

为ItemsControl设置背景的最简单方法是将其包装在Border(或网格)中,

The easiest way to set a background for your ItemsControl would be to wrap it in a Border (or Grid, they're essentially the same now) and set a background brush on that instead.

我不建议您按照示例进行操作,但是如果您希望Background属性起作用,这是您需要做的事情:

I don't recommend you do what follows for your example, but this is what you would need to do if you wanted the Background property to work:

<ItemsControl Background="Red">
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Grid Background="{TemplateBinding Background}">
                <ItemsPresenter/>
            </Grid>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

这篇关于ItemControl背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆