Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模板化wpf控件中的内容对齐和定位_Wpf_Templates_Location - Fatal编程技术网

模板化wpf控件中的内容对齐和定位

模板化wpf控件中的内容对齐和定位,wpf,templates,location,Wpf,Templates,Location,我是VB.NET和C#中的专业Windows应用程序开发人员,使用Windows窗体,现在非常高兴能够参与WPF开发。这里是我无法通过搜索解决的第一个问题: 我编写了以下模板以完全更改按钮的外观: <ControlTemplate TargetType="{x:Type Button}" x:Key="ImageButtonTemplate"> <Grid> <Image Name="img" Source="/PSP

我是VB.NET和C#中的专业Windows应用程序开发人员,使用Windows窗体,现在非常高兴能够参与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没有显示效果(我想知道为什么)

此外,有时我想将文本定位到某个位置,例如,当backgound图像中有一个位于左侧直到x=60的符号时,x=70

我绕了一圈,在按钮内嵌入了一个标签,我可以定位,但我认为一定有一个更干净的解决方案

希望我说清楚。顺便说一句,WPF rulez——和这个网站一样,我通过在这里发帖(来自谷歌)解决了大部分问题:)

提前感谢,


朱利安这里发生了一些事情。第一个问题是您为ContentPresenter设置了绝对大小(通常,在WPF中,您会避免控件的绝对大小和绝对位置,但当您来自WinForms时,这是一个很难打破的习惯)。如果删除,则可以通过设置ContentPresenter本身的垂直对齐和水平对齐来正确对齐。在按钮本身上设置垂直对齐和水平对齐不起作用的原因是,您没有在模板中的任何位置使用这些属性。理想情况下,您可以这样做:

<ContentPresenter VerticalAlignment="{TemplateBinding VerticalAlignment}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}"/>

现在,您可以在使用此模板的按钮上设置对齐方式。当然,如果您总是希望此模板居中,您可以对其进行硬编码,而不是使用TemplateBinding


通常,在替换模板时要记住的是,当WPF呈现控件时,它将忽略未显式绑定到模板中的控件的任何属性。如果你不告诉它如何处理这些属性,它就不会猜测。一个罕见的例外是您的示例中的content属性,您可以不显式绑定它,因为WPF将假定您打算将它放在ContentPresenter中!但是所有其他按钮属性都将被忽略。例如,如果您尝试更改模板按钮的Background属性,它将被忽略,因为WPF不知道将该背景放置在何处。它应该是整个控制吗?它应该是你的网格吗?只是你的内容演示者?你必须说出来。

谢谢,这很有帮助。我将它改为:VerticalAlignment=“{TemplateBinding VerticalContentAlignment}”HorizontalAlignment=“{TemplateBinding HorizontalContentAlignment}”,然后它做得很好。@Julian:NP。我投票赞成你的问题是为了帮你一点忙!