![](/img/trans.png)
[英]Value is out of Range Exception While setting ResourceDictionary Source to CustomControl's generic.XAML
[英]Binding to readonly property in generic.xaml for customControl (Button)
我正在使用generic.xaml和依賴項屬性進行CustomControl(按鈕)。
這是我的generic.xaml代碼:
<Style TargetType="{x:Type local:FlatButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:FlatButton}">
<Grid MinHeight="50" MaxHeight="50" MinWidth="200" MaxWidth="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="{TemplateBinding BackgroundDarker}">
</Grid>
<Grid Grid.Column="1" Background="{TemplateBinding Background}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe UI" Foreground="White" FontWeight="Bold" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的customControl類別:
Public Shared Shadows BackgroundProperty As DependencyProperty = DependencyProperty.Register("Background", GetType(SolidColorBrush), GetType(FlatButton))
Public Overloads Property Background As SolidColorBrush
Get
Return CType(GetValue(BackgroundProperty), SolidColorBrush)
End Get
Set(value As SolidColorBrush)
SetValue(BackgroundProperty, value)
End Set
End Property
Public Shared BackgroundDarkerProperty As DependencyProperty = DependencyProperty.Register("BackgroundDarker", GetType(SolidColorBrush), GetType(FlatButton))
Public ReadOnly Property BackgroundDarker As SolidColorBrush
Get
Return Background.Darker
End Get
End Property
最后,我如何在UserControl中使用控件:
<Grid>
<local:FlatButton Background="Red" />
</Grid>
當我在FlatButton的xaml中放置“紅色”時,右側部分用紅色很好地着色(在VS和運行時中),但是我想要的是左側部分自動用深紅色自動着色(這是一個有效的擴展名) )。 但是它似乎沒有顏色。 我在輸出中沒有綁定錯誤。
我究竟做錯了什么 ?
謝謝大家
-----編輯----- :
好的,為此,我制作了一個轉換器,將“背景”值轉換為較深的顏色。 我使用轉換器的實例將左側網格的背景綁定到“背景”。
雖然轉換器可以工作,但是此功能屬於FlatButton
控制代碼內部。 使用Background的PropertyChangedCallback
更新BackgroundDarker。
public class FlatButton : Button
{
// Background
public static new DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(SolidColorBrush), typeof(FlatButton), new PropertyMetadata(OnBackgroundChanged));
public new SolidColorBrush Background { get { return (SolidColorBrush)GetValue(BackgroundProperty); } set { SetValue(BackgroundProperty, value); } }
// BackgroundDarker
public static DependencyProperty BackgroundDarkerProperty = DependencyProperty.Register("BackgroundDarker", typeof(SolidColorBrush), typeof(FlatButton));
public SolidColorBrush BackgroundDarker { get { return (SolidColorBrush)GetValue(BackgroundDarkerProperty); } private set { SetValue(BackgroundDarkerProperty, value); } }
private static void OnBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// update BackgroundDarker
var btn = (FlatButton)d;
btn.BackgroundDarker = btn.Background.Darker();
}
static FlatButton()
{
// lookless control, get default style from generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatButton), new FrameworkPropertyMetadata(typeof(FlatButton)));
}
}
public static class SolidColorBrushExtension
{
public static SolidColorBrush Darker(this SolidColorBrush brush)
{
const double perc = 0.6;
return new SolidColorBrush(Color.FromRgb((byte)(brush.Color.R * perc), (byte)(brush.Color.G * perc), (byte)(brush.Color.B * perc)));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.