簡體   English   中英

Silverlight(而非WPF)中的嵌套樣式(如CSS)吊墜

[英]Pendant for Nested Styles (like CSS) in Silverlight instead of WPF

我的任務是在每個StackPanel中嵌套的每個Button上設置20px的邊距。

在WPF中,我在Application.Resources中使用以下代碼:

<Style TargetType="StackPanel">
  <Style.Resources>
    <Style TargetType="Button">
        <Setter Property="Margin" Value="20" />
    </Style>
  </Style.Resources>
</Style>

在Silverlight中,缺少“ Style.Resources”標簽。 但是我嘗試了這段代碼:

<Style TargetType="StackPanel">
  <Setter Property="Resources">
    <Setter.Value>
        <ResourceDictionary>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="20" />
            </Style>
        </ResourceDictionary>
    </Setter.Value>
  </Setter>
</Style>

可悲的是,在Silverlight方面沒有任何反應(沒有錯誤,沒有結果)。

有誰知道在Silverlight中是否可能沒有手動在每個Stackpanel上通過鍵設置樣式?

在Silverlight中,不能簡單地完成,可以使用附加的依賴項屬性來完成。 這是此類屬性,可以執行您想要的操作-將條目添加到元素ResourceDictionaries。

namespace SilverlightApplication1.Assets
{
    public class Utils : DependencyObject
    {
        public static readonly DependencyProperty AdditionalResourcesProperty = DependencyProperty.RegisterAttached(
            "AdditionalResources", typeof(ResourceDictionary), typeof(Utils), new PropertyMetadata(null, OnAdditionalResourcesPropertyChanged));

        public static ResourceDictionary GetAdditionalResources(FrameworkElement obj)
        {
            return (ResourceDictionary)obj.GetValue(AdditionalResourcesProperty);
        }

        public static void SetAdditionalResources(FrameworkElement obj, ResourceDictionary value)
        {
            obj.SetValue(AdditionalResourcesProperty, value);
        }

        private static void OnAdditionalResourcesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement element = d as FrameworkElement;
            if (element == null)
                return;

            ResourceDictionary oldValue = e.OldValue as ResourceDictionary;
            if (oldValue != null)
            {
                foreach (DictionaryEntry entry in oldValue)
                {
                    if (element.Resources.Contains(entry.Key) && element.Resources[entry.Key] == entry.Value)
                        element.Resources.Remove(entry.Key);
                }
            }

            ResourceDictionary newValue = e.NewValue as ResourceDictionary;
            if (newValue != null)
            {
                foreach(DictionaryEntry entry in newValue)
                {
                    element.Resources.Add(entry.Key, entry.Value);
                }
            }
        }
    }
}

它的用法與您已經嘗試過的非常相似:

<Style TargetType="StackPanel">
    <Setter Property="assets:Utils.AdditionalResources">
        <Setter.Value>
            <ResourceDictionary>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="20" />
                </Style>
            </ResourceDictionary>
        </Setter.Value>
    </Setter>
</Style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM