c# - 更改 WPF DatePicker 文本框的外观

标签 c# wpf datepicker controltemplate

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

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

有没有一种更简洁的方法可以在不对整个控件进行模板化的情况下做到这一点? 有没有办法只对命名部分(即 PART_TextBox)进行模板化?

最佳答案

您可以更改 DatePickerTextBox 样式

代码

<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>

请引用此链接 Custom WPF DatePickerTextBox Template Help

关于c# - 更改 WPF DatePicker 文本框的外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42437732/

相关文章:

c# - 日期类型 c#.net 上的比较验证器

c# - View 订阅 ViewModel 中实例对象的属性

c# - 在 wpf 中动态更改按钮值的问题

c# - 在 Generic.xaml 中使用 DataTemplate 进行 WPF 自定义控件

javascript - beforeShowDay 每天调用两次

twitter-bootstrap - Bootstrap 日期选择器在 Mozilla Firefox 中将 Undefined Nan 显示为标题

c# - 公共(public)静态只读字段与查找列表的 getter

c# - 路线总是去第一个 maproute

c# - 超链接上的投影效果

javascript - Bootstrap 日期选择器 endDate