Silverlight默认内容Presenter内容 [英] Silverlight Defaulting ContentPresenter Content

查看:115
本文介绍了Silverlight默认内容Presenter内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不起作用?

generic.xaml 中用于自定义控件:

采用应用于自定义控件的样式...

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}" />

这是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);

如您所见,它可以带任何对象。我尝试将其更改为Grid,但这无济于事。

As you can see, it takes any object. I tried changing it to a Grid, but that did not help.

它引发此错误(来自javascript):_未能分配给属性'System.Windows.Controls。 ContentPresenter.Content'

It throws this error (from javascript): _Failed to assign to property 'System.Windows.Controls.ContentPresenter.Content'

奇怪的是,如果我从设置器中删除Grid并仅使用文本,以下操作会很好:

Oddly, the following will work fine if I remove the Grid from the setter nd just use text:

<Setter Property="ChromeContent" Value="DEFAULT" />

此外,这也可以通过控件类中的OnApplyTemplate方法运行:

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;

我很难理解是什么阻止了网格的默认内容, generic.xaml从工作。有人对此事有任何了解吗?

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?

在此先感谢您的帮助!

推荐答案

这是问题所在:-

<Setter Property="ChromeContent">
  <Setter.Value>
    <Grid />
  </Setter.Value>
</Setter>

您不应直接在 UIElement 中添加资源字典或样式的值。您可能会认为样式是某种描述符,但事实并非如此。样式中的值是它们保存的对象的构造实例。您的样式拥有一个 Grid 的实例。每当使用该样式将其分配给 ChromeContent 属性时,它将尝试使用同一单个 Grid实例。

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 of Grid. Whenever that style is used to assign to a ChromeContent property it will attempt to assing the same single instance of the Grid.

A UIElement 只能是一个父级的子级。如果构造了您的控件的两个实例会怎样?可能会(如果让Silverlight允许您)尝试将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.

这是一个原因适用于 ControlTemplate DataTemplate 之类的模板。每次使用模板时都会调用其中的标记,而不是在首次解析Xaml时调用。

This is one reason for templates such as ControlTemplate and DataTemplate. The markup inside these is invoked each time the template is used rather than when the Xaml is first parsed.

编辑

要回答您的补充问题,您应默认设置另一个类型为 DataTemplate 的属性:-

To answer you supplementary question, you should default another property of type DataTemplate:-

<Setter Property="ChromeContentTemplate">
  <Setter.Value>
    <DataTemplate>
      <Grid />
    </DataTemplate>
  </Setter.Value>
</Setter>

属性:-

public Object ChromeContentTemplate
{
  get { return (DataTemplate)GetValue(ChromeContentTemplateProperty); }
  set { SetValue(ChromeContentTemplateProperty, value); }
}

public static readonly DependencyProperty ChromeContentTemplateProperty=
    DependencyProperty.Register("ChromeContentTemplate", typeof(DataTemplate), 
    typeof(casPopup), null);

控制模板:-

<ContentPresenter Grid.Column="0" 
     x:Name="ChromeContentPresenter" 
     Content="{TemplateBinding ChromeContent}"
     ContentTemplate="{TemplateBinding ChromeContentTemplate" />

这篇关于Silverlight默认内容Presenter内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆