Привязка значения свойства стиля к элементам пользовательского шаблона внутри стиля
Пытаюсь сделать стиль для CheckBox'а с границами разного цвета. Но, не понимаю, почему не работает привязка значения Setter'а (BorderThickness) к Rectangle (Width и Height) для масштабирования толщины границ через свойство BorderThickness у CheckBox'а.
XAML:
<Window.Resources>
<Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="#5333"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
<DockPanel x:Name="checkBoxBorder" LastChildFill="False" Background="{TemplateBinding Background}">
<Rectangle Fill="Yellow" DockPanel.Dock="Left" Width="{TemplateBinding BorderThickness}"/>
<Rectangle Fill="Red" DockPanel.Dock="Top" Height="{TemplateBinding BorderThickness}"/>
<Rectangle Fill="Green" DockPanel.Dock="Right" Width="{TemplateBinding BorderThickness}"/>
<Rectangle Fill="Blue" DockPanel.Dock="Bottom" Height="{TemplateBinding BorderThickness}"/>
</DockPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Background="#5777">
<CheckBox Width="200" Height="200" Style="{DynamicResource CheckBoxStyle1}" BorderThickness="5"/>
</Grid>
Ответы (1 шт):
Автор решения: aepot
→ Ссылка
TemplateBinding здесь не работает, я не разбирался, почему. Но самое главное - вы пытаетесь свойство Width типа double привязать к свойству BorderThickness типа Thickness. Не находите это странным?
Ну и Rectangle здесь не к месту.
<Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="#5333"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid Background="Transparent" SnapsToDevicePixels="True">
<DockPanel LastChildFill="False" Background="{TemplateBinding Background}">
<Border Background="Yellow" DockPanel.Dock="Left" Width="{Binding BorderThickness.Left, RelativeSource={RelativeSource AncestorType=CheckBox}}"/>
<Border Background="Red" DockPanel.Dock="Top" Height="{Binding BorderThickness.Top, RelativeSource={RelativeSource AncestorType=CheckBox}}"/>
<Border Background="Green" DockPanel.Dock="Right" Width="{Binding BorderThickness.Right, RelativeSource={RelativeSource AncestorType=CheckBox}}"/>
<Border Background="Blue" DockPanel.Dock="Bottom" Height="{Binding BorderThickness.Left, RelativeSource={RelativeSource AncestorType=CheckBox}}"/>
</DockPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<CheckBox Width="200" Height="200" Style="{StaticResource CheckBoxStyle1}" BorderThickness="5"/>