【问题标题】:Make the children of DataTemplate inherit color of parents使 DataTemplate 的子级继承父级的颜色
【发布时间】:2011-06-23 06:51:14
【问题描述】:

我有一个用于 ListViewItems 的 DataTemplate,其中包含一些 TextBoxes 和 Combos。像这样的:

<DataTemplate x:Key="ListTemplate">
        <Border Margin="2, 2, 4, 2" BorderThickness="2, 0, 0, 0">
            <Grid>
                (...)
                <StackPanel Grid.Column="0" Grid.Row="0">
                    <TextBox VerticalAlignment="Bottom" Name="hoursField" Margin="5">
                        <TextBox.Text>
                            <Binding Path="Hours" />
                        </TextBox.Text>
                    </TextBox>
 ...

现在,我想让 TextBox 继承他容器的背景颜色,以便使用与 StackPanel 相同的背景颜色进行绘制,并在每次选择 ListView 时将 TextBoxes 的 Foreground 更改为白色。

关于我如何获得这个的任何线索?

【问题讨论】:

  • 不是自动做的吗?

标签: c# .net wpf xaml wpf-controls


【解决方案1】:

如果您不想弄乱除了要添加的控件之外的任何东西,请将它们的背景颜色设置为透明。

Hacky,但会工作。

【讨论】:

    【解决方案2】:

    假设你的问题是:

    a.) 我想让 TextBox 继承他的容器的背景颜色。

    回答:需要为ListBoxItem ControlTemplate设置根网格的背景颜色。这将由放置在 ListBoxItem 内的内容呈现器继承。

    b.) 每次选择 ListView 时将 TextBoxes 的 Foreground 更改为白色

    答案:在 ListBoxItem ControlTemplate 中将根网格的前景更改为选中状态下的白色

    【讨论】:

      【解决方案3】:

      如果您的父级的背景颜色发生变化,您还可以使用绑定来明确告诉文本框绑定包含控件,使用绑定中的 ElementName。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-28
        • 2015-06-09
        • 1970-01-01
        • 2012-07-13
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多