访问样式元素

时间:2017-05-11 02:03:56

标签: c# wpf xaml styles templatebinding

我有一个滑块样式(剪切版本)

<Style x:Key="SliderThumb" TargetType="Thumb">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Thumb">
                <Ellipse Height="15" Width="15" Fill="White"></Ellipse>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ControlTemplate x:Key="Slider" TargetType="Slider">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Track Grid.Row="1" x:Name="PART_Track"   >
            <Track.DecreaseRepeatButton>
                <RepeatButton Style="{StaticResource SliderRepeatButton1}"  Command="Slider.DecreaseLarge" />
            </Track.DecreaseRepeatButton>
            <Track.Thumb>
                <Thumb Style="{StaticResource SliderThumb}"  />
            </Track.Thumb>
            <Track.IncreaseRepeatButton>
                <RepeatButton Style="{StaticResource SliderRepeatButton}" Command="Slider.IncreaseLarge" />
            </Track.IncreaseRepeatButton>
        </Track>
    </Grid>
</ControlTemplate>

<Style x:Key="BigSliderStyle" TargetType="Slider">
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Style.Triggers>
        <Trigger Property="Orientation" Value="Horizontal">
            <Setter Property="MinHeight" Value="21" />
            <Setter Property="MinWidth" Value="104" />
            <Setter Property="Template" Value="{StaticResource Slider}" />
        </Trigger>
    </Style.Triggers>
</Style>

从我的界面中我将其创建为

<Slider Style="{StaticResource BigSliderStyle}"/>

我需要将工具提示绑定到仅与椭圆相关的滑块。 有没有办法从表单XAML中访问椭圆属性?

1 个答案:

答案 0 :(得分:0)

这可以解决您的问题:

<Style x:Key="SliderThumb" TargetType="Thumb">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="ToolTip"><!--just add this part to your code-->
        <Setter.Value>
            <TextBlock Text="Some text"/><!-- also you can use Text="{Binding propertyName}"-->
        </Setter.Value>
    </Setter><!--just add this part to your code-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Thumb">
                <Ellipse Height="15" Width="15" Fill="White"></Ellipse>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

您已经可以通过该样式访问拇指。该元素支持开箱即用的工具提示,因此无需关注ellipse,因为template占位符只有thumb。 这也允许您使用Slider的不同模板在整个应用中保留ToolTip