有没有一种方法可以将资源字典值链接到WinUI XAML中的属性

1wnzp6jl  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(96)

我用XAML为一个滑块控件写了下面的代码,注意到foregroundbackground属性只在不悬停在slider track上和不点击它的时候设置slider track的颜色。
所以我必须编写这个Slider.Resources部分来访问这些资源并覆盖它们。

<Slider 
    x:Name="mySlider"  
    StepFrequency="0.1" 
    Minimum="0" 
    Maximum="8" 
    SnapsTo="StepValues" 
    Foreground="#b400ff" 
    Background="#6c4080">
    <Slider.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Dark">
                    <!-- Using foreground colour -->
                    <SolidColorBrush x:Key="SliderTrackValueFillPointerOver" Color="#b400ff"/>
                    <SolidColorBrush x:Key="SliderTrackValueFillPressed" Color="#b400ff"/>

                    <!-- Using background colour -->
                    <SolidColorBrush x:Key="SliderTrackFillPointerOver" Color="#6c4080"/>
                    <SolidColorBrush x:Key="SliderTrackFillPressed" Color="#6c4080"/>
                </ResourceDictionary>
                <ResourceDictionary x:Key="Light">
                    <!-- Using foreground colour -->
                    <SolidColorBrush x:Key="SliderTrackValueFillPointerOver" Color="#b400ff"/>
                    <SolidColorBrush x:Key="SliderTrackValueFillPressed" Color="#b400ff"/>
                    
                    <!-- Using background colour -->
                    <SolidColorBrush x:Key="SliderTrackFillPointerOver" Color="#6c4080"/>
                    <SolidColorBrush x:Key="SliderTrackFillPressed" Color="#6c4080"/>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>
        </ResourceDictionary>
    </Slider.Resources>
</Slider>

字符串
我有其他滑块需要不同的颜色作为背景,但作为XAML和WinUI的新手,每次都这样定义感觉有点过分。有没有一种方法可以说我项目中的所有滑块都应该使用foregroundbackground的颜色作为其资源字典部分中列出的值?

dpiehjr4

dpiehjr41#

您可以定义一个custom attached property,让您以可重用的方式以编程方式应用资源:

public class ThemeService : DependencyObject
{
    public static readonly DependencyProperty ThemeProperty =
        DependencyProperty.RegisterAttached(
          "Theme",
          typeof(SliderTheme),
          typeof(ThemeService),
          new PropertyMetadata(null, OnSet)
        );

    public static void SetTheme(Slider element, SliderTheme value) =>
        element.SetValue(ThemeProperty, value);

    public static SliderTheme GetTheme(Slider element) =>
        (SliderTheme)element.GetValue(ThemeProperty);

    private static void OnSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Slider slider = (Slider)d;
        if (e.NewValue is SliderTheme sliderTheme)
        {
            slider.Foreground = sliderTheme.Foreground;
            slider.Background = sliderTheme.Background;

            slider.Resources["SliderTrackValueFillPointerOver"] = sliderTheme.Foreground;
            slider.Resources["SliderTrackValueFillPressed"] = sliderTheme.Foreground;
            slider.Resources["SliderTrackFillPointerOver"] = sliderTheme.Background;
            slider.Resources["SliderTrackFillPressed"] = sliderTheme.Background;
        }
    }
}

public class SliderTheme
{
    public Brush Foreground { get; set; }
    public Brush Background { get; set; }
}

字符串

用法:

<Slider 
    x:Name="mySlider"  
    StepFrequency="0.1" 
    Minimum="0" 
    Maximum="8" 
    SnapsTo="StepValues">
    <local:ThemeService.Theme>
        <local:SliderTheme Foreground="#b400ff" Background="#6c4080" />
    </local:ThemeService.Theme>
</Slider>

<Slider 
    x:Name="myOtherSlider"  
    StepFrequency="0.1" 
    Minimum="0" 
    Maximum="8" 
    SnapsTo="StepValues">
    <local:ThemeService.Theme>
        <local:SliderTheme Foreground="Red" Background="Yellow" />
    </local:ThemeService.Theme>
</Slider>

sz81bmfz

sz81bmfz2#

你可以把它作为Page资源:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush
                    x:Key="SliderTrackValueFillPointerOver"
                    Color="#b400ff" />
                <SolidColorBrush
                    x:Key="SliderTrackValueFillPressed"
                    Color="#b400ff" />
                <SolidColorBrush
                    x:Key="SliderTrackFillPointerOver"
                    Color="#6c4080" />
                <SolidColorBrush
                    x:Key="SliderTrackFillPressed"
                    Color="#6c4080" />
            </ResourceDictionary>
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush
                    x:Key="SliderTrackValueFillPointerOver"
                    Color="#b400ff" />
                <SolidColorBrush
                    x:Key="SliderTrackValueFillPressed"
                    Color="#b400ff" />
                <SolidColorBrush
                    x:Key="SliderTrackFillPointerOver"
                    Color="#6c4080" />
                <SolidColorBrush
                    x:Key="SliderTrackFillPressed"
                    Color="#6c4080" />
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Page.Resources>

字符串

相关问题