在控件中,我应该在哪里找到列出所有状态的属性?

3

我正在查看winrt项目的模板,其中返回按钮具有以下样式:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal" />
        <VisualState x:Name="PointerOver">
            ...
        </VisualState>
        <VisualState x:Name="Pressed">
           ...
        </VisualState>
        <VisualState x:Name="Disabled">
            ...
        </VisualState>
    </VisualStateGroup>
    <VisualStateGroup x:Name="FocusStates">
        <VisualState x:Name="Focused">
            ...
        </VisualState>
        <VisualState x:Name="Unfocused" />
        <VisualState x:Name="PointerFocused" />
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

我假设上述VisualStates是按钮状态,但我无法弄清楚这在按钮对象上是如何被跟踪的,以及框架如何将状态绑定到视觉状态。
我已经在互联网上四处寻找更好的理解方法,但一直没有结果。请帮助我理解这些如何紧密联系在一起。我知道你可以从代码后台手动转到特定状态,但似乎这里有一个约定我不明白。
1个回答

4

没有列出控件状态的属性。

根据MSDN文档,控件作者必须提供一个控件契约,以便ControlTemplate作者知道在模板中放置什么内容。

控件契约具有三个元素:

  • 控件逻辑使用的可视元素。
  • 控件的状态及每个状态归属的组。
  • 影响控件外观的公共属性。

可视元素和状态都作为类属性提供。

[TemplatePart(Name = "XXX", Type = typeof(RepeatButton))]
[TemplatePart(Name = "YYY", Type = typeof(RepeatButton))]
[TemplateVisualState(Name = "Focused", GroupName = "FocusedStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusedStates")]

您应该查看默认的控件样式和模板,我认为您需要的所有数据都在那里。

如果您必须在运行时获取数据,则可以使用反射来获取给定类的属性,例如:

System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);

for (int i = 0; i < attributes.Length; i++)
{
  if (attributes[i] is TemplatePart || attributes[i] is TemplateVisualState)
  {
     System.Console.WriteLine(((TemplateVisualState) attributes[i]).Name);
  }   
}

阅读这篇 MSDN 文章,它将使事情更加清晰明了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接