【发布时间】:2011-01-28 14:52:37
【问题描述】:
为什么这不起作用?
在 generic.xaml 中用于自定义控件:
在应用于自定义控件的样式中...
<Setter Property="ChromeContent">
<Setter.Value>
<Grid />
</Setter.Value>
</Setter>
...
稍后,在控件模板中...
<ContentPresenter Grid.Column="0"
x:Name="ChromeContentPresenter"
Content="{TemplateBinding ChromeContent}" />
这是 ChromeContent 的依赖属性...
public Object ChromeContent
{
get { return (Object)GetValue(ChromeContentProperty); }
set { SetValue(ChromeContentProperty, value); }
}
public static readonly DependencyProperty ChromeContentProperty =
DependencyProperty.Register("ChromeContent", typeof(Object),
typeof(casPopup), null);
如您所见,它需要任何对象。我尝试将其更改为 Grid,但这没有帮助。
它抛出这个错误(来自 javascript):_Failed to assign to property 'System.Windows.Controls.ContentPresenter.Content'
奇怪的是,如果我从 setter 中删除 Grid 并只使用文本,则以下操作会正常工作:
<Setter Property="ChromeContent" Value="DEFAULT" />
此外,这也适用于控件类中的 OnApplyTemplate 方法:
Grid g = new Grid();
g.Width = 100;
g.Height = 25;
g.Background = new SolidColorBrush(Colors.LightGray);
ChromeContent = g;
我很难理解是什么阻止了在 generic.xaml 中定义的网格的默认内容工作。有人知道这件事吗?
非常感谢您的帮助!
【问题讨论】:
标签: silverlight custom-controls default contentpresenter