我在資源字典中定義了一個 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?
uj5u.com熱心網友回復:
無法將動態資源分配給依賴屬性的默認值。(這也沒有意義,因為默認值被評估一次,而動態資源可以根據定義更改)
但是,如果您的目標是將實際的依賴屬性值分配給動態資源,將屬性有效地系結到動態資源,則此站點上已經存在描述如何執行此操作的答案。這似乎是現有的最佳解決方案。
我將其發布為答案而不是標記為重復,因為您的問題專門詢問設定默認值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485880.html
