Button에서 파생 된 CustomControl을 단순히 색상이 지정된 사각형으로 표시하려고합니다. 내 컨트롤에 설정할 수있는 두 가지 속성, 정상적인 색 (ColdColor) 및 마우스가 컨트롤 (HotColor) 위에있을 때 사용되는 다른 색을 지정하고 싶습니다.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"/>
편집 : 대답 "그이 내 코드입니다 does not work "는 TestCustomControl이 완전히 투명하다는 것을 의미합니다.
감사합니다. 가 작동하지 않습니다. 즉, TestCustomControl이 완전히 투명합니다. – NPVN
새로운 아이디어를 포함하는 제안에 의해 편집했습니다 –
감사합니다. 잔뜩 작동합니다! Border.Background 요소에 지정된 브러시에서 작동한다는 것을 의미합니다. ColorAnimation는 아무것도 실시하지 않습니다. 컨트롤은 계속해서 ColdColor로 그려집니다. 편집 : 나는 애니메이션에 같은 변화를 적용했다. 물론 Path를 HotColor로 바꾸었다. – NPVN