簡體   English   中英

在WPF中動態設置控制模板

[英]Setting Control Template Dynamically in WPF

 <s:FastLineRenderableSeries.PointMarker>

  <s:SpritePointMarker PointMarkerTemplate="{StaticResource AnyWpfControlTemplateCanGoHere}"/>

根據用戶從許多選項中選擇的內容,我需要在運行時設置此模板。 模板已經在通用字典中定義,就像這樣

<ControlTemplate x:Key="RightTriangleAnnotations">
   <Polygon Points="0 0, 20 20,0 20" x:Name="poly" Fill="Green" Stroke="Red"></Polygon>

我的代碼是這樣的

  var pointMarker = new SpritePointMarker();
  pointMarker.Template = **Here the template should go**
  renderSeries.PointMarker = pointMarker;

知道如何在運行時設置模板。

基本上,這取決於資源的定義位置。 您只需要從字典中獲取模板。 例如,如果您的字典是為應用程序定義的:

Application.Current.Resources["RightTriangleAnnotations"] as ControlTemplate

或者,如果字典直接附加到XAML中,則可以使用任何可視對象,例如SpritePointMarker :: Resources 請參考此問題以獲取詳細的解決方案: 如何從C#代碼訪問wpf中的ResourceDictionary?

或者,如果未在任何XAML文件中指定字典,則必須先加載字典,然后將其分配給可視對象,以便能夠通過鍵獲取所需的資源。 檢查此問題以獲取詳細信息:以編程方式訪問ResourceDictionary項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM