请教我如何正确使用ControlTemplate?
好吧,我想我对使用 ControlTemplate 很菜鸟,哈哈...
我想要的只是模板化我的每个 UserControl,这样每个 UserControl 都会有删除、保存、取消按钮。
所以现在我正在写一些测试...... 尝试在所有内容周围制作边框...
在 App.xaml
<Application.Resources>
<ControlTemplate x:Key="DeleteSaveCancelTemplate">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Border BorderThickness="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black">
<ContentPresenter/>
</Border>
</Grid>
</ControlTemplate>
</Application.Resources>
然后在 UserControl 中:
<UserControl ... Height="150" Width="300" Template="{StaticResource DeleteSaveCancelTemplate}" >
但我看到的只是...黑色...我做错了什么?我想我应该使用 ContentPresenter 来显示内容?
Ok, I think I am pretty noob on using ControlTemplate lol...
All I want is to template my every UserControl so every UserControl will have Delete, Save, Cancel buttons.
So now I am writting some test......
Try to make a border around the everything...
In App.xaml
<Application.Resources>
<ControlTemplate x:Key="DeleteSaveCancelTemplate">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Border BorderThickness="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black">
<ContentPresenter/>
</Border>
</Grid>
</ControlTemplate>
</Application.Resources>
Then in UserControl:
<UserControl ... Height="150" Width="300" Template="{StaticResource DeleteSaveCancelTemplate}" >
But all I see is just ...black... what did I do wrong? I thought I should use ContentPresenter to shows the content?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于大多数控件,您可以使用 ContentPresenter。 ItemsPresenter 仅适用于扩展 ItemsControl 的元素。
For most controls you use ContentPresenter. ItemsPresenter only applies to elements that extend ItemsControl.
所以...我从此页面发现了问题哈哈
默认情况下,ControlPresenter 不绑定到内容...
我需要写
除非您指定了 TargetType,否则它将默认绑定。
So... I found the problem lol From this page
The ControlPresenter doens't bind to Content by default...
I need to write
Unless you have specify the TargetType, then you it will bind by default.