比较来自世界各地的卖家的域名和 IT 服务价格

自定义模板 WPF DatePickerTextBox

我正在尝试使用用户
TextBox

在控制元件中
DatePicker

, 但我不能将日期与弹出日历绑定到
TextBox

. 我不想体调整体
DatePicker

没有极端的必要性,和
DatePickerTextBox

它有自己的控制,因此必须有一种方法只能改变它。 以下代码 - 这就是初学者所拥有的:


<style targettype="{x:Type DatePickerTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DatePickerTextBox}">
<TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</style>


也许我做了绑定错误,或者
PART_TextBox

可能是错误的,因为它不是模板本身的一部分
DatePicker

.

任何人,请帮忙! :/

先感谢您!
已邀请:

诸葛浮云

赞同来自:

试试吧:


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

帅驴

赞同来自:

我明白这个问题长期以来一直是答案,但是直接绑定到文本财产 DatePicker 会允许
https://msdn.microsoft.com/en- ... .aspx
在你的控制模板中,很容易观察短暂的/提供了长格式
https://msdn.microsoft.com/en- ... .aspx
.


<datepicker>
<datepicker.resources>
<style targettype="{x:Type DatePickerTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</style>
</datepicker.resources>
</datepicker>


"PART_TextBox" 也没有必要,因为它不是模板的一部分 DatePickerTextBox. 单身的 PARTs, 其中包含 DatePickerTextBox, - 这是:


[TemplatePart/Name = DatePickerTextBox.ElementContentName, Type = typeof/ContentControl//]
public sealed partial class DatePickerTextBox : TextBox

private const string ElementContentName = "PART_Watermark";


并继承了ot。
https://msdn.microsoft.com/en- ... .aspx
...


[TemplatePart/Name = "PART_ContentHost", Type = typeof/FrameworkElement//] 
public abstract class TextBoxBase : Control

internal const string ContentHostTemplateName = "PART_ContentHost";


替代解决方案:

如果你拒绝使用
https://msdn.microsoft.com/en- ... .aspx
并使用遗传 PART, 你可以改变
https://msdn.microsoft.com/en- ... .aspx
如果不更改默认控制功能。


<datepicker>
<datepicker.resources>
<style targettype="{x:Type DatePickerTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"/>

<ScrollViewer Name="PART_ContentHost"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</style>
</datepicker.resources>
</datepicker>

二哥

赞同来自:

如果您声明了一个元素 XAML /1/, 然后在演示中浏览它
Design

, 您可以右键单击 /2/ 并出口他的模板 /3/:

1


<window ...attributes...="">
<grid>
<datepickertextbox></datepickertextbox>
</grid>
</window>


2

https://i.stack.imgur.com/Smczx.png
3


<style targettype="{x:Type DatePickerTextBox}" x:key="DatePickerTextBoxStyle1">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DatePickerTextBox}">
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="WatermarkBrush" Color="#FFAAAAAA"/>
</Grid.Resources>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
<VisualTransition GeneratedDuration="0:0:0.1" To="MouseOver"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Duration="0" To="#FF99C1E2" Storyboard.TargetProperty="/Border.BorderBrush/./SolidColorBrush.Color/" Storyboard.TargetName="ContentElement"/>
<ColorAnimation Duration="0" To="#FF99C1E2" Storyboard.TargetProperty="/Border.BorderBrush/./SolidColorBrush.Color/" Storyboard.TargetName="watermark_decorator"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="WatermarkStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Unwatermarked"/>
<VisualState x:Name="Watermarked">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentElement"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_Watermark"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="Focused">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisual"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="1" Opacity="1" Padding="{TemplateBinding Padding}">
<Grid x:Name="WatermarkContent" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Border x:Name="ContentElement" BorderBrush="#FFFFFFFF" BorderThickness="1"/>
<Border x:Name="watermark_decorator" BorderBrush="#FFFFFFFF" BorderThickness="1">
<ContentControl x:Name="PART_Watermark" Focusable="False" IsHitTestVisible="False" Opacity="0" Padding="2"/>
</Border>
<ScrollViewer x:Name="PART_ContentHost" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Border x:Name="FocusVisual" BorderBrush="#FF45D6FA" CornerRadius="1" IsHitTestVisible="False" Opacity="0"/>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</style>

要回复问题请先登录注册