有没有办法绑定到模板控件的属性?

时间:2019-04-13 18:03:37

标签: wpf controltemplate

我想将ListViewItem的背景色设置为该项目的DataTemplate中使用的颜色。

这是DataTemplates之一,请注意已设置BackGround颜色:

<DataTemplate DataType="{x:Type viewmodel:DataPointViewModel}">
    <UniformGrid Columns="2" Background="LimeGreen">
        <TextBlock Text="{Binding Name}"/>
        <TextBlock Text="{Binding Description}"/>
    </UniformGrid>
</DataTemplate>

这是带有边框的ItemContainerStyle,设置为ListView的ItemContainerStyle:

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="Yellow"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding BackGround}">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想避免ViewModel本身具有Background颜色。到目前为止,我所有的努力都导致了Bindings中的绑定错误,例如:

{Binding BackGround, RelativeSource={RelativeSource AncestorType=UniformGrid}}

在将Binding设置为“ TemplateBinding BackGround”的情况下,它确实成功使用了我在样式本身中设置的颜色,这正是我所期望的。

如何绑定到DataTemplate中的BackGround属性,或者这是无法实现的?

0 个答案:

没有答案
相关问题