Reputation: 23833
I have the following WPF Style
for a custom control
<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>
with the IValueConverter
as
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();
}
}
I am attempting to set the padding of the control so my indicator can be centered correctly. I want the padding of the control to be half the IndicatorSize
set in the parent style. Currently, I am just trying to get it to be the IndicatorSize
, but the binding I am attempting does not work as expected.
Padding="{Binding Path=DataContext.IndicatorSize,
RelativeSource={RelativeSource AncestorType={x:Type local:TransportControl}},
Converter={StaticResource ValueToHorizontalPaddingConverter}}"
What am I doing wrong?
Thanks for your time.
Upvotes: 0
Views: 199
Reputation: 1118
You can use TemplateBinding to do that:
Padding="{TemplateBinding IndicatorSize, Converter={StaticResource ValueToHorizontalPaddingConverter}}"
Another way of centering the indicator would be to give the border a name, get a reference to it in your custom control's OnApplyTemplate(..) method and set its Padding in C# whenever the IndicatorSize property changes. This way you would not need the binding and converter.
Upvotes: 1