Buttonから派生したCustomControlを作成しようとしていますが、これは単に色付きの矩形として表示されます。私は、通常の色(ColdColor)とマウスがコントロール(HotColor)の上にあるときに使用される別の色を指定することができます私のコントロール上に私のコントロール上の2つのプロパティを持っています。ControlTemplateのプロパティにブラシの色をバインドします。
私は、ブラシの色とコントロールのプロパティの間でバインディングを設定する方法を理解できません。これは私のコードです:
Generic.xaml:
<Style TargetType="{x:Type local:TestCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TestCustomControl}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Name="MyBorder">
<Border.Background>
<!-- This works: -->
<!--<SolidColorBrush Color="Green" />-->
<!-- This doesn't work: -->
<SolidColorBrush Color="{TemplateBinding ColdColor}" />
</Border.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<!-- This works: -->
<!--<ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="Red" Duration="0:0:0.2"/>-->
<!-- This doesn't work: -->
<ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="{TemplateBinding HotColor}" Duration="0:0:0.2"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TestCustomControl.cs:MainWindow.xamlで
public class TestCustomControl : Button
{
static TestCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl)));
}
public Color HotColor
{
get { return (Color)GetValue(HotColorProperty); }
set { SetValue(HotColorProperty, value); }
}
// Using a DependencyProperty as the backing store for HotColor. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HotColorProperty =
DependencyProperty.Register("HotColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(new Color()));
public Color ColdColor
{
get { return (Color)GetValue(ColdColorProperty); }
set { SetValue(ColdColorProperty, value); }
}
// Using a DependencyProperty as the backing store for ColdColor. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ColdColorProperty =
DependencyProperty.Register("ColdColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(new Color()));
}
は使用方法:
<my:TestCustomControl ColdColor="#FF0000AF" HotColor="#FFFF00AF"/>
EDIT:それを言うために」 does not work "は、TestCustomControlが完全に透明であることを意味します。
ありがとうございますは動作しません。つまり、TestCustomControlは完全に透過的です。 – NPVN
新しいアイデアを含めるように提案で編集しました –
ありがとうございました:その途中で動作します! Border.Background要素で指定されたブラシで動作することを意味します。 ColorAnimationは何もしません。コントロールは引き続きColdColorでペイントされます。編集:アニメーションに同じ変更を適用しました。もちろん、PathをHotColorに変更しました。 – NPVN