【问题标题】:WP7 Set the Datepicker border colorWP7 设置 Datepicker 边框颜色
【发布时间】:2011-02-04 00:48:10
【问题描述】:

我无法在 Toolkit Datepicker 中设置边框颜色,因为它显示在页面上。我不是在谈论 DatePickerPage。我可以设置背景色和前景色,但边框不成立。

<toolkit:DatePicker x:Name="dpDeliverBy" Header="Deliver By" Grid.Row="6" 
HeaderTemplate="{StaticResource MyHeaderTemplate}" Margin="-12, 0, 0, -10" Value=""
BorderBrush="Black" Background="White" BorderThickness="2" Foreground="Black" />

边框似乎没有固定,我不知道要使用什么其他属性。

【问题讨论】:

    标签: silverlight xaml windows-phone-7 silverlight-toolkit


    【解决方案1】:

    您无法编辑DatePicker 模板是一件有趣的事情。我通过查看source code 发现,由于某种原因,它的发生是因为模板是在主控件主题中定义的 - Generic.xaml 并且它本身没有定义BorderBrush 属性。

    下载包 - 您将需要它在现有骨架之上构建自定义控件。

    您应该打开该主题文件,如果您查看DatePicker 的模板,您可以编辑BorderBrushBorderThickness 的值。要记住的一件事 - 一旦重新编译源代码,您需要确保使用正确的库(在主项目中引用时)。默认情况下,当您添加 Microsoft.Phone.Controls.Toolkit 单元时,它将引用在 GAC 中注册的库(假设您安装了工具包)并采用位于 SDK 文件夹中的库- 你不想要那个。更改库名称或更改本地副本。

    如果你需要这方面的教程,I just wrote one

    这里是修改后的样式(在源代码中修改,方便复用):

    <Style TargetType="controls:DatePicker">
        <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Azure"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="PickerPageUri" Value="/Microsoft.Phone.Controls.Toolkit;component/DateTimePickers/DatePickerPage.xaml"/>
        <Setter Property="ValueStringFormat" Value="{}{0:d}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:DatePicker">
                    <StackPanel>
                        <ContentControl
                            Content="{TemplateBinding Header}"
                            ContentTemplate="{TemplateBinding HeaderTemplate}"
                            Foreground="{StaticResource PhoneSubtleBrush}"
                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                            Margin="12,0,12,-4"/>
                        <Button
                            x:Name="DateTimeButton"
                            Content="{TemplateBinding ValueString}"
                            Background="{TemplateBinding Background}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            FontFamily="{TemplateBinding FontFamily}"
                            Foreground="{TemplateBinding Foreground}"
                            Height="72"
                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

      【解决方案2】:

      不需要重新构建 Silverlight Toolkit 也不需要达到预期的效果。 Toolkit 采用的方法与自 XAML 诞生以来一直使用的方法相同:即“无外观”控件。控件的逻辑在类文件中定义,视觉外观在 Themes\generic.xaml 文件中的 XAML 中定义。

      框架使用此文件中的 XAML 呈现控件,除非您指定替换,这是一种常见的做法。因此,您只需在 App.xaml 中添加新样式,然后您无需重新编译 Toolkit 程序集,因为框架将使用您的自定义样式而不是新样式。

      如果你想了解更多,VSJ 上有一篇很好的文章解释了这个模型。

      【讨论】:

      • 这真的取决于 - 为了将来重复使用,保持属性打开并能够在没有模板的情况下进行设置会更容易。
      • 我同意应该向 Silverlight Toolkit 团队提出源代码更改建议,但总的来说,从长远来看,当您可以轻松地重新模板化控件时自定义第三方库更易于维护 :)
      • 我想这是个人意见的问题 - 我不想在每个新项目中重新模板化控件,但话又说回来 - 这取决于您实际需要多久自定义一次准备就绪(使用兼容的主题)控件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 2015-11-29
      • 2014-03-24
      相关资源
      最近更新 更多