【发布时间】:2013-02-18 19:38:54
【问题描述】:
我在 XAML 中定义了以下样式:
<Style TargetType="telerik:RadDiagramShape" x:Key="styleShapeBase">
<Setter Property="Width" Value="120" />
<Setter Property="Height" Value="60" />
<Setter Property="IsResizingEnabled" Value="False" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock x:Name="lblName" Text="{Binding Name}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
然后在代码隐藏中我分配数据上下文。我想绘制一个形状,其中包含来自对象的一些文本(如果这最终起作用,我将在那里放置更多信息)。我是这样做的:
var shape = new RadDiagramShape();
shape.Style = (Style)Resources["styleShapeBase"];
shape.DataContext = item.DataContext;
item 是一个简单的 POCO,它具有类型为 string 的 Name 属性(这部分有效,我已经跟踪它,即正确分配了 DataContext)。
但数据绑定永远不会发生。是否是设计使然(即内容模板内没有数据绑定),如果不是有什么问题?谢谢,
【问题讨论】:
-
尝试改用 TemplateBinding。这是一个示例:stackoverflow.com/questions/1877047/…
-
TemplateBinding 只在控制模板的主体中起作用,所以在这里不相关。
标签: silverlight xaml data-binding styles