更改 WPF DatePicker 文本框的外观

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42437732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:01:30  来源:igfitidea点击:

Change appearance of WPF DatePicker's textbox

c#wpfdatepickercontroltemplate

提问by Simon Baylis

Changing the Foreground property of the WPF DatePicker does change the colour of the text in the text box, but changing the Background property doesn't. But you can change it by styling the contained DatePickerTextBox. So I ended up with this:

更改 WPF DatePicker 的 Foreground 属性确实会更改文本框中文本的颜色,但更改 Background 属性不会。但是您可以通过设置包含的 DatePickerTextBox 的样式来更改它。所以我最终得到了这个:

<DatePicker Foreground="Yellow" >
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}" >
            <Setter Property="Background" Value="Black" />
        </Style>
    </DatePicker.Resources>
</DatePicker>

Is there a tidier way of doing this without templating the whole control? Is there a way to just template the named part i.e. PART_TextBox?

有没有更整洁的方法来做到这一点而不对整个控件进行模板化?有没有办法只对命名部分进行模板化,即 PART_TextBox?

回答by Ragavan

You can change DatePickerTextBox style

您可以更改 DatePickerTextBox 样式

Code

代码

<DatePicker>
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox x:Name="PART_TextBox" 
                                    Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.Resources>
</DatePicker>

Please refer this link Custom WPF DatePickerTextBox Template Help

请参阅此链接自定义 WPF DatePickerTextBox 模板帮助

回答by Hyman

This is also an issue with Combo Boxes. You can add a trigger to fix this as shown.

这也是组合框的问题。您可以添加触发器来解决此问题,如图所示。

    <Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyle">
        <Setter Property="Foreground" Value="WhiteSmoke"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsEnabled, 
                RelativeSource={RelativeSource AncestorType=ComboBox}}" Value="True">
                <Setter Property="Foreground" Value="Black"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsEnabled, 
                RelativeSource={RelativeSource AncestorType=DatePicker}}" Value="True">
                <Setter Property="Foreground" Value="Black"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

回答by mm8

Is there a tidier way of doing this without templating the whole control? Is there a way to just template the named part i.e. PART_TextBox?

有没有更整洁的方法来做到这一点而不对整个控件进行模板化?有没有办法只对命名部分进行模板化,即 PART_TextBox?

No. You can't set any properties of an element that is defined inside a control's template (and that is not bound to any properties of the "parent" control itself) without re-defining the entire template or use implicit styles.

不可以。如果不重新定义整个模板或使用隐式样式,则不能设置在控件模板内定义的元素的任何属性(并且未绑定到“父”控件本身的任何属性)。