找到你要的答案

Q:[UWP]Custom control visualstate not work

Q:[有限]自定义控件编程能力不行

I create a custom control in my windows 10 UWP app called CustomControl1

public sealed class CustomControl1 : ButtonBase
{
    public CustomControl1()
    {
        this.DefaultStyleKey = typeof(CustomControl1);
    }
}

And the default style in Generic.xaml

<Style TargetType="local:CustomControl1" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CustomControl1">
                <Border>
                    <TextBlock x:Name="textBlock" Text="This is a text block"/>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="VisualStateGroup">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver">
                                <VisualState.Setters>
                                    <Setter Target="textBlock.Foreground" Value="Red"/>
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <VisualState.Setters>
                                    <Setter Target="textBlock.TextBlock" Value="Blue"/>
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The problem is the TextBlock's foreground don't change to Red when mouse over nor change to Blue when pressed. But if i change CustomControl1's base class to Button it works fine.

public sealed class CustomControl1 : Button
{
    public CustomControl1()
    {
        this.DefaultStyleKey = typeof(CustomControl1);
    }
}

What's different between the two cases?

我在我的Windows 10的应用程序称为customcontrol1 UWP创建自定义控件

public sealed class CustomControl1 : ButtonBase
{
    public CustomControl1()
    {
        this.DefaultStyleKey = typeof(CustomControl1);
    }
}

在Generic.xaml的默认样式

<Style TargetType="local:CustomControl1" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CustomControl1">
                <Border>
                    <TextBlock x:Name="textBlock" Text="This is a text block"/>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="VisualStateGroup">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver">
                                <VisualState.Setters>
                                    <Setter Target="textBlock.Foreground" Value="Red"/>
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <VisualState.Setters>
                                    <Setter Target="textBlock.TextBlock" Value="Blue"/>
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

问题是TextBlock的前景不更改为红色时,鼠标按下时也变蓝。但如果我改变customcontrol1的基类按钮效果不错。

public sealed class CustomControl1 : Button
{
    public CustomControl1()
    {
        this.DefaultStyleKey = typeof(CustomControl1);
    }
}

这两种情况有什么不同?

answer1: 回答1:

Because Button has the visual states you used, and ButtonBase doesn't. So when you write that template for ButtonBase, the control itself doesn't activate any visual states and therefore your text color doesn't change. Button on the other hand does, and color changes.

Because Button has the visual states you used, and ButtonBase doesn't. So when you write that template for ButtonBase, the control itself doesn't activate any visual states and therefore your text color doesn't change. Button on the other hand does, and color changes.

xaml  windows-10  uwp