WPF DatePicker的奇怪外观

时间:2013-07-09 21:19:18

标签: wpf .net-4.0 datepicker

在我的.NET 4.0上的WPF应用程序中,我在水平DatePicker中使用了两个TextBlock和两个StackPanel,它看起来像这样:

enter image description here

在左侧DatePicker,我尝试了更高的字体大小。除此之外,没有特殊的设置或风格。

按钮与TextBlock对齐,但DatePicker中的文字不是。

知道如何更改日期文字的对齐方式吗?

编辑:这是我的XAML:

    <StackPanel Orientation="Horizontal"
                HorizontalAlignment="Right" VerticalAlignment="Bottom"
                >
        <TextBlock Text="from " />
        <DatePicker SelectedDate="{Binding FromDate, Mode=TwoWay}" FontSize="14" />
        <TextBlock Text=" until " />
        <DatePicker SelectedDate="{Binding ToDate, Mode=TwoWay}" />
    </StackPanel>

2 个答案:

答案 0 :(得分:6)

感谢Jorge,它是App.xaml中的Button样式:

    <Style TargetType="{x:Type Button}">
        <Setter Property="Margin" Value="0 5 10 5" />
    </Style>

此样式也适用于DatePicker内的按钮。

答案 1 :(得分:1)

假设您正在处理文本块,那么您可以尝试这样的事情。

    <StackPanel Orientation="Horizontal" Height="40" Margin="10">
        <Border Background="Transparent" BorderBrush="#FF2A2A89" BorderThickness="2">
        <TextBlock Width="150" Text="Testing" TextAlignment="Center"
                   VerticalAlignment="Center" />
        </Border>
        <DatePicker VerticalContentAlignment="Center" Name="datepicker" />
    </StackPanel>

它的内容实际上仅用于演示。你应该得到这样的东西:

enter image description here

但是,如果您只是显示日期,为什么不使用默认设置的datepicker?它默认显示所选日期。

另一种方法是,您可以使用文本框而不是文本块,并将IsReadOnly设置为 真正。我假设您正在使用文本块,因此它们不可编辑。使用文本框,您可以设置VerticalContentAlignment =“Center”,就是这样。