Silverlight 默认 ContentPresenter 内容
为什么这不起作用?
在自定义控件的 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);
正如您所见可以看到,它需要任何物体。我尝试将其更改为网格,但这没有帮助。
它抛出此错误(来自 javascript): _Failed to allocate to property 'System.Windows.Controls.ContentPresenter.Content'
奇怪的是,如果我从设置器中删除网格并仅使用文本,以下内容将正常工作:
<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 中定义的网格的默认内容工作。有人对此事有了解吗?
非常感谢您的帮助!
Why won't this work?
In generic.xaml for a custom control:
In the style applied to the custom control...
<Setter Property="ChromeContent">
<Setter.Value>
<Grid />
</Setter.Value>
</Setter>
...
Later, in the control template...
<ContentPresenter Grid.Column="0"
x:Name="ChromeContentPresenter"
Content="{TemplateBinding ChromeContent}" />
Here's the dependency property for 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);
As you can see, it takes any object. I tried changing it to a Grid, but that did not help.
It throws this error (from javascript): _Failed to assign to property 'System.Windows.Controls.ContentPresenter.Content'
Oddly, the following will work fine if I remove the Grid from the setter nd just use text:
<Setter Property="ChromeContent" Value="DEFAULT" />
Also, this will work too from the OnApplyTemplate method in the control class:
Grid g = new Grid();
g.Width = 100;
g.Height = 25;
g.Background = new SolidColorBrush(Colors.LightGray);
ChromeContent = g;
I'm having a hard time understanding what is preventing the default content of a grid, defined in the generic.xaml from working. Does anyone have any knowledge on this matter?
Many thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是问题所在:-
您不应将
UIElement
直接包含在资源字典中或作为样式的值。您可能会将样式视为某种描述符,但事实并非如此。样式中的值是它们所持有的对象的构造实例。您的样式包含Grid
的单个实例。每当使用该样式分配给ChromeContent
属性时,它都会尝试分配相同的网格单个实例。UIElement
只能是一个父级的子级。如果构造您的控件的两个实例,会发生什么情况?将会(如果 silverlight 允许的话)尝试将 Grid 的同一个单个实例分配给两个控件。这是使用
ControlTemplate
和DataTemplate
等模板的原因之一。每次使用模板时(而不是首次解析 Xaml 时)都会调用其中的标记。编辑:
要回答您的补充问题,您应该默认另一个
DataTemplate
类型的属性:-属性:-
控件模板:-
This is the problem:-
You should not include a
UIElement
directly in a resource dictionary or as a value of a style. You might see the style as being some kind of descriptor but it isn't. The values in a style are constructed instances of the objects they hold. Your style holds a single instance ofGrid
. Whenever that style is used to assign to aChromeContent
property it will attempt to assing the same single instance of the Grid.A
UIElement
can only be a child of one parent. What would happen if two instances your control were constructed? There would (if silverlight let you) be an attempt to assign the same single instance of the Grid to both controls.This is one reason for templates such as
ControlTemplate
andDataTemplate
. The markup inside these is invoked each time the template is used rather than when the Xaml is first parsed.Edit:
To answer you supplementary question, you should default another property of type
DataTemplate
:-Property:-
Control Template:-