[英]WPF – how to attach VisualStateGroups in control template created in code-behind?
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
…
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
…
</Grid>
</ControlTemplate>
之前的XAML 控制模板的替代方案是代碼隱藏 C#中的以下示例。
var rootGrid = new FrameworkElementFactory(typeof(Grid));
var controlTemplate = new ControlTemplate(typeof(TreeViewItem))
{
VisualTree = rootGrid
};
如何在代碼隱藏版本中附加VisualStateGroup
? 我假設我需要以FrameworkElementFactory
為目標。 方法FrameworkElementFactory.AppendChild()
需要另一個FrameworkElementFactory
。
FrameworkElementFactory 是根據Microsoft 文檔創建控件模板的一種已棄用的方法。
此 class 是一種已棄用的以編程方式創建模板的方法,模板是 FrameworkTemplate 的子類,例如 ControlTemplate 或 DataTemplate; 使用此 class 創建模板時,並非所有模板功能都可用。 The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.
您可以在代碼中將模板構建為字符串,然后使用XamlReader
的Load
方法。 這將使 VisualStateGroups 的實現變得容易!
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.