wpf - 在父样式 WPF 中设置属性值
问题描述
我有以下Style
用于自定义控件的 WPF
<Style TargetType="{x:Type local:TransportControl}">
<Setter Property="MinorTickBrush" Value="{DynamicResource BlackBrush}"/>
<Setter Property="MajorTickBrush" Value="{DynamicResource BlackBrush}"/>
<Setter Property="IndicatorBrush" Value="{DynamicResource BlckBrush}"/>
<Setter Property="ProgressBorderBrush" Value="{DynamicResource BlackBrush}"/>
<Setter Property="ProgressBrush" Value="{DynamicResource HighlightBrush}"/>
<Setter Property="IndicatorSize" Value="16"/>
<Setter Property="IndicatorBrush" Value="{DynamicResource BlackBrush}"/>
<Setter Property="IndicatorGlow" Value="True"/>
<Setter Property="IndicatorGlowBrush" Value="GhostWhite"/>
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TransportControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{Binding Path=DataContext.IndicatorSize,
RelativeSource={RelativeSource AncestorType={x:Type local:TransportControl}},
Converter={StaticResource ValueToHorizontalPaddingConverter}}"
Margin="4,2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Canvas Name="PART_TimelineCanvas" Grid.Row="0" Height="20" ClipToBounds="False"/>
<Canvas Name="PART_ProgressCanvas" Grid.Row="1" ClipToBounds="False"/>
<Canvas Name="PART_IndicatorCanvas"
Grid.Row="0"
Grid.RowSpan="2"
ClipToBounds="False"
Panel.ZIndex="2"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
与IValueConverter
作为
public class ValueToHorizontalPaddingConverter : IValueConverter
{
public object Convert(object value, Type targetType, object format, CultureInfo culture)
{
double padding = System.Convert.ToDouble(value);
return new Thickness(padding, 0, padding, 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我正在尝试设置控件的填充,以便我的指示器可以正确居中。我希望控件的填充是IndicatorSize
父样式中设置的一半。目前,我只是想让它成为IndicatorSize
,但我尝试的绑定没有按预期工作。
Padding="{Binding Path=DataContext.IndicatorSize,
RelativeSource={RelativeSource AncestorType={x:Type local:TransportControl}},
Converter={StaticResource ValueToHorizontalPaddingConverter}}"
我究竟做错了什么?
谢谢你的时间。
解决方案
您可以使用 TemplateBinding 来做到这一点:
Padding="{TemplateBinding IndicatorSize, Converter={StaticResource ValueToHorizontalPaddingConverter}}"
使指示器居中的另一种方法是为边框命名,在自定义控件的 OnApplyTemplate(..) 方法中获取对它的引用,并在 IndicatorSize 属性更改时在 C# 中设置其填充。这样您就不需要绑定和转换器。
推荐阅读
- ruby - Activeldap:为活动目录中的多值属性添加值
- java - java.lang.string 类型的值 br 无法转换为 jsonarray
- c++ - C++ 演示函数同时使用作为指针的 const 参数这是为什么?
- java - 从多个值中找到最佳比率以最小化框的成本(100 % 或更低)
- java - 在 java 中使用任务执行器将 1000 万条记录插入数据库
- java - 将 iCSee 添加到 Java 项目
- python - 需要更多理解python语句:- return ["even", "odd"][num % 2]
- module - 如何使用另一个目录中的模块?
- java - 如何在循环中创建 JPanel 并将它们插入 JPanel Array [] []
- node.js - 我可以在 Mongo/Mongoose 中使用 $pull 并更新同一数组中的剩余元素吗?