为什么这个不能工作?
在自定义控件的 generic.xaml 中:
在应用于自定义控件的样式中...
<Setter Property="ChromeContent">
<Setter.Value>
<Grid />
</Setter.Value>
</Setter>
稍后,在控制模板中...
<ContentPresenter Grid.Column="0"
x:Name="ChromeContentPresenter"
Content="{TemplateBinding ChromeContent}" />
以下是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):“无法分配给属性'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中定义的网格默认内容的工作。有人对此有什么了解吗?
非常感谢您的帮助!