[英]Is there a way to assign a dynamic resource to a dependency property in code
我在資源字典中定義了一個 SolidColorBrush,如下所示:
<Color x:Key="ColorGray100">#F5F6F7</Color>
<SolidColorBrush x:Key="BrushGray100" Color="{DynamicResource ColorGray100}" />
我在后面的代碼中為用戶控件定義了一個依賴屬性,如下所示:
/// <summary>
/// get/set the brush for the pattern ring.
/// The pattern ring is a full circle which the progress ring is drawn over.
/// This Brush value is defaulted to Brushes.LightGray
/// </summary>
public Brush PatternRingBrush
{
get => (Brush)GetValue(PatternRingBrushProperty);
set => SetValue(PatternRingBrushProperty, value);
}
public static readonly DependencyProperty PatternRingBrushProperty =
DependencyProperty.Register("PatternRingBrush", typeof(Brush), typeof(UXProgressRing),
new UIPropertyMetadata(Brushes.LightGray));
如何將“BrushGray100”SolidColorBrush 指定為默認 UIPropertyMetadata 值而不是 Brushes.LightGray?
無法將動態資源分配給依賴屬性的默認值。 (這也沒有意義,因為默認值被評估一次,而動態資源可以根據定義更改)
但是,如果您的目標是將實際的依賴屬性值分配給動態資源,將屬性有效地綁定到動態資源,則此站點上已經存在描述如何執行此操作的答案。 這似乎是現有的最佳解決方案。
我將其發布為答案而不是標記為重復,因為您的問題專門詢問設置默認值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.