Default Styles/ControlThemes

This commit is contained in:
ClemensFischer 2024-07-05 09:20:30 +02:00
parent 4608f9a511
commit dd7a4814af
8 changed files with 34 additions and 67 deletions

View file

@ -62,7 +62,7 @@ namespace MapControl
{
var newProperty = property.AddOwner<TOwner>();
if (!Equals(defaultValue, newProperty.GetMetadata(typeof(TOwner)).DefaultValue))
if (!Equals(defaultValue, property.GetMetadata(typeof(TOwner)).DefaultValue))
{
newProperty.OverrideMetadata<TOwner>(new StyledPropertyMetadata<TValue>(defaultValue));
}
@ -75,13 +75,6 @@ namespace MapControl
return newProperty;
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
AvaloniaProperty property)
where TOwner : AvaloniaObject
{
return AddOwner<TOwner, TValue>((StyledProperty<TValue>)property);
}
public static void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding)
{
target.Bind(property, binding);

View file

@ -3,29 +3,22 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:MapControl">
<ControlTheme TargetType="{x:Type map:MapBase}" x:Key="{x:Type map:MapBase}">
<Setter Property="Background" Value="White"/>
<ControlTheme TargetType="map:MapBase" x:Key="{x:Type map:MapBase}">
<Setter Property="Foreground" Value="Black"/>
</ControlTheme>
<ControlTheme TargetType="{x:Type map:Map}" x:Key="{x:Type map:Map}" BasedOn="{StaticResource {x:Type map:MapBase}}"/>
<ControlTheme TargetType="map:Map" x:Key="{x:Type map:Map}" BasedOn="{StaticResource {x:Type map:MapBase}}"/>
<ControlTheme TargetType="{x:Type map:MapItemsControl}" x:Key="{x:Type map:MapItemsControl}">
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type map:MapItemsControl}">
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}"/>
</ControlTemplate>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<map:MapPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</ControlTheme>
<ControlTemplate TargetType="ContentControl" x:Key="ContentControlTemplate">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
<ControlTheme TargetType="{x:Type ContentControl}" x:Key="ContentControlTheme">
<ControlTheme TargetType="ContentControl" x:Key="ContentControlTheme">
<Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource AncestorType=map:MapBase}}"/>
<Setter Property="BorderBrush" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=map:MapBase}}"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
@ -33,54 +26,27 @@
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="ClipToBounds" Value="False"/>
<!-- Template Setter here somehow disables a Template Setter in a Style for a derived type, e.g. MapItem -->
<!--<Setter Property="Template">
<ControlTemplate TargetType="{x:Type ContentControl}">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
</Setter>-->
<!-- Setter here somehow disables a Template Setter in a Style for a derived type, e.g. MapItem -->
<!--<Setter Property="Template" Value="{StaticResource ContentControlTemplate}"/>-->
</ControlTheme>
<ControlTheme TargetType="{x:Type map:MapContentControl}" x:Key="{x:Type map:MapContentControl}" BasedOn="{StaticResource ContentControlTheme}">
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type map:MapContentControl}">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
</Setter>
<ControlTheme TargetType="map:MapContentControl" x:Key="{x:Type map:MapContentControl}" BasedOn="{StaticResource ContentControlTheme}">
<Setter Property="Template" Value="{StaticResource ContentControlTemplate}"/>
</ControlTheme>
<ControlTheme TargetType="{x:Type map:MapItem}" x:Key="{x:Type map:MapItem}" BasedOn="{StaticResource ContentControlTheme}">
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type map:MapItem}">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
</Setter>
<ControlTheme TargetType="map:MapItem" x:Key="{x:Type map:MapItem}" BasedOn="{StaticResource ContentControlTheme}">
<Setter Property="Template" Value="{StaticResource ContentControlTemplate}"/>
</ControlTheme>
<ControlTheme TargetType="{x:Type map:Pushpin}" x:Key="{x:Type map:Pushpin}" BasedOn="{StaticResource ContentControlTheme}">
<ControlTheme TargetType="map:Pushpin" x:Key="{x:Type map:Pushpin}" BasedOn="{StaticResource ContentControlTheme}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="CornerRadius" Value="5"/>
<Setter Property="Padding" Value="7,5"/>
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type map:Pushpin}">
<ControlTemplate TargetType="map:Pushpin">
<map:PushpinBorder
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Background="{TemplateBinding Background}"

View file

@ -13,6 +13,8 @@ using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
#endif
using System;
namespace MapControl
{
/// <summary>

View file

@ -93,14 +93,20 @@ namespace MapControl
public static DependencyProperty AddOwner<TOwner, TValue>(
DependencyProperty property,
TValue defaultValue = default,
Action<TOwner, TValue, TValue> changed = null)
where TOwner : DependencyObject
{
FrameworkPropertyMetadata metadata = null;
var metadata = new FrameworkPropertyMetadata();
if (!Equals(defaultValue, property.GetMetadata(typeof(TOwner)).DefaultValue))
{
metadata.DefaultValue = defaultValue;
}
if (changed != null)
{
metadata = new FrameworkPropertyMetadata((o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue));
metadata.PropertyChangedCallback = (o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue);
}
return property.AddOwner(typeof(TOwner), metadata);

View file

@ -75,6 +75,7 @@ namespace MapControl
static MapBase()
{
BackgroundProperty.OverrideMetadata(typeof(MapBase), new FrameworkPropertyMetadata(Brushes.White));
ClipToBoundsProperty.OverrideMetadata(typeof(MapBase), new FrameworkPropertyMetadata(true));
DefaultStyleKeyProperty.OverrideMetadata(typeof(MapBase), new FrameworkPropertyMetadata(typeof(MapBase)));
}

View file

@ -14,7 +14,7 @@ namespace MapControl
DependencyPropertyHelper.AddOwner<MapItem, bool>(MapPanel.AutoCollapseProperty);
public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty,
DependencyPropertyHelper.AddOwner<MapItem, Location>(MapPanel.LocationProperty, null,
(item, oldValue, newValue) => item.UpdateMapTransform(newValue));
static MapItem()

View file

@ -16,7 +16,7 @@ namespace MapControl
}
public static readonly DependencyProperty DataProperty =
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty,
DependencyPropertyHelper.AddOwner<MapPath, Geometry>(Path.DataProperty, null,
(path, oldValue, newValue) => path.DataPropertyChanged(oldValue, newValue));
public Geometry Data

View file

@ -4,7 +4,6 @@
xmlns:map="clr-namespace:MapControl">
<Style TargetType="map:MapBase">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
</Style>