父样式WPF中的属性值设置值

时间:2019-03-11 09:55:00

标签: wpf xaml mvvm

我具有以下用于自定义控件的WPF Style

<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}}" 

我在做什么错?

感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:1)

您可以使用TemplateBinding来做到这一点

Padding="{TemplateBinding IndicatorSize, Converter={StaticResource ValueToHorizontalPaddingConverter}}"

将指示器居中的另一种方法是给边框命名,在您的自定义控件的OnApplyTemplate(..)方法中获得对它的引用,并在IndicatorSize属性更改时在C#中设置其填充。这样,您将不需要绑定和转换器。