我是一名专业的Windows应用程序开发人员,在VB.NET和C#中使用Windows窗体,现在非常高兴能够进入WPF开发。在这里我的第一个问题我无法通过搜索解决:模板化wpf控件中的内容对齐和定位
我写了下面的模板,以彻底改变我的按钮的外观:
<ControlTemplate TargetType="{x:Type Button}" x:Key="ImageButtonTemplate">
<Grid>
<Image Name="img" Source="/PSPlayer;component/Images/Buttons/ButtonNormal_normal.png"/>
<ContentPresenter Stylus.IsTapFeedbackEnabled="False" Stylus.IsTouchFeedbackEnabled="False" Stylus.IsPressAndHoldEnabled="False" Stylus.IsFlicksEnabled="False" Width="98" Height="61"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="img" Property="Source" Value="/PSPlayer;component/Images/Buttons/ButtonNormal_MouseOver.png"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="img" Property="Source" Value="/PSPlayer;component/Images/Buttons/ButtonNormal_MouseDown.png"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
这里按钮定义:
<Button Content="SomeText" Height="61" Margin="0,0,0,7" Name="button7" Width="98" Click="button7_Click" Template="{StaticResource ImageButtonTemplate}"/>
问题是,文本“SomeText”出现在按钮的左上角,我无法更改该位置。按钮的属性HorizontalContentAlignment和VerticalContentAlignment不显示效果(我想知道为什么)。
此外,我想将文本定位到某个位置,如x = 70,当背景图像中有一个符号位于左侧时,直到x = 60。
我做了一个标签嵌入按钮,我可以定位,但认为必须有一个更清洁的解决方案的walkaround。
希望我说清楚了。 WPF rulez的方式 - 以及这个网站,我的大部分问题,我解决了张贴在这里(从谷歌推出):)提前
感谢,
朱利安
谢谢,这有帮助。我将它改为: VerticalAlignment =“{TemplateBinding VerticalContentAlignment}”HorizontalAlignment =“{TemplateBinding HorizontalContentAlignment}” 然后它做得很好。 – Julian 2012-04-10 13:21:29
似乎我需要一些声誉来投票你的答案。 – Julian 2012-04-10 17:10:30
@Julian:NP。我投了你的问题来帮助你一点点! – 2012-04-10 17:36:55