wpf - 有没有办法绑定到模板化控件的属性?
问题描述
我想将 ListViewItem 的背景颜色设置为 Item 的 DataTemplate 中使用的颜色。
这是 DataTemplate 之一,请注意设置的背景颜色:
<DataTemplate DataType="{x:Type viewmodel:DataPointViewModel}">
<UniformGrid Columns="2" Background="LimeGreen">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Description}"/>
</UniformGrid>
</DataTemplate>
这是带有Border的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 本身中有背景颜色。到目前为止,我所有的努力都导致了来自绑定的绑定错误,例如:
{Binding BackGround, RelativeSource={RelativeSource AncestorType=UniformGrid}}
将 Binding 设置为“TemplateBinding BackGround”后,它确实成功地使用了我在 Style 本身中设置的颜色,这正是我所期望的。
如何绑定到 DataTemplate 中的 BackGround 属性,或者这无法实现?
解决方案
推荐阅读
- foreach - 我想在 foreach 循环中使用两种模型方法
- angular - PrimeNg Table 切换单元格编辑
- android - 在网格视图中选择单元格的任何方式
- angular - Angular RxJS:自定义 pipable - Observable<{}> 和 Observable
- django - 客户端发送 ws://[host]:[port]/path 到服务器,我的服务器是 Nginx+uwsgi+Django,但是当我启动 get to server 时总是遇到 404 错误
- python - 有没有办法只在非递归函数中做某事?
- xml - 是否可以在 XSL1.0 中从开始日期和结束日期获得 YEAR+WEEKNUMBER 的代码?
- android - 如何将最大长度应用于视图直到第二个视图到达父端
- android - 更新到 android Gradle 3.4.0 后出现 Gradle 错误
- base64 - 如何在无聊的 SSL 中使用不推荐使用的函数?