我是一名專業的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