WPF:决定在用户控件中的何处显示contentPresenter
我有一个非常简单的用户控件,带有一些边框和一些装饰,可以对鼠标悬停、按下和一些不错的东西做出反应 我想允许人们从外部设置文本的内容,但当我设置内容时,生成的内容呈现器会覆盖我的整个WPF结构 这就是我迄今为止所尝试的:WPF:决定在用户控件中的何处显示contentPresenter,wpf,user-controls,contentpresenter,Wpf,User Controls,Contentpresenter,我有一个非常简单的用户控件,带有一些边框和一些装饰,可以对鼠标悬停、按下和一些不错的东西做出反应 我想允许人们从外部设置文本的内容,但当我设置内容时,生成的内容呈现器会覆盖我的整个WPF结构 这就是我迄今为止所尝试的: <UserControl tags tags tags> <!-- This is the only way I found to style the textblock that WPF --> <!-- generates to
<UserControl tags tags tags>
<!-- This is the only way I found to style the textblock that WPF -->
<!-- generates to encapsulate the content string -->
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
<TextBlock Background="Green"
HorizontalAligment="Center"
VerticalAlignment="Center"
Text = "{TemplateBinding Content}"/>
</ControlTemplate>
</UserControl.Template>
<!-- Here my borders and fancy things. I want to add animations -->
<!-- and react to mouseOver and Pressed like a button -->
<Border x:Name="SuperNiceBorder" tag tag tag>
<HERE I WANT THE CONTENTPRESENTER>
</Border>
</UserControl>
有没有办法告诉WPF我想要用户在内容中设置的文本 在ControlTemplate中移动所有动画和触发器 用ContentPresenter替换TextBlock:
<UserControl x:Class="MySolution.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
<Border x:Name="MyBorder" Background="Green">
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content = "{TemplateBinding Content}"/></Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="MyBorder" Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Template>
</UserControl>
您可以使用UserControl,如以下示例所示:
1:
<local:MyControl Content="Test Testing tester"/>
<local:MyControl>
<TextBlock Text="Another test from a TextBlock"/>
</wpfAllPurposesTest:MyControl>
2:
<local:MyControl Content="Test Testing tester"/>
<local:MyControl>
<TextBlock Text="Another test from a TextBlock"/>
</wpfAllPurposesTest:MyControl>
UserControl.Content=object,TextBlock.Text=string。这是行不通的。为什么不使用ContentPresenter而不是TextBlock,并绑定内容而不是文本?如何为按下的按钮添加触发器?@javirs查看本教程,其中解释了可能的触发器