我通过Validation.ErrorTemplate
设置DataTrigger
属性。该模板包含我Border
周围的DatePicker
和内置Border
的第二TextBlock
。
当Border
DatePickerTextBox
属性仅为IsFocused
时,我试图看到第二个True
。
我的代码下面到目前为止(它只是一个缩短的版本):
<Style x:Key="CustomDatePickerStyle" TargetType="{x:Type DatePicker}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DatePicker}">
<Border Background="{DynamicResource Control.Background}" Padding="0" BorderThickness="0" CornerRadius="3">
<Button x:Name="PART_Button" Grid.Column="1" Foreground="{TemplateBinding Foreground}" Focusable="False" HorizontalAlignment="Left" Margin="3,0,3,0" Grid.Row="0" Template="{StaticResource DropDownButtonTemplate}" VerticalAlignment="Center" Width="20"/>
<DatePickerTextBox x:Name="PART_TextBox" Height="14" Margin="3 0 0 0" Grid.Column="0" Focusable="{TemplateBinding Focusable}"
HorizontalContentAlignment="Stretch" Grid.Row="0" VerticalContentAlignment="Center">
<DatePickerTextBox.Template>
<ControlTemplate TargetType="DatePickerTextBox">
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="3" Opacity="1">
<Grid x:Name="WatermarkContent" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Border x:Name="ContentElement" BorderBrush="Red" BorderThickness="0"/>
<ScrollViewer x:Name="PART_ContentHost" Background="{TemplateBinding Background}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Border x:Name="FocusVisual" BorderBrush="Red" CornerRadius="3" IsHitTestVisible="False" Opacity="0"/>
</Grid>
</Border>
</ControlTemplate>
</DatePickerTextBox.Template>
</DatePickerTextBox>
</Grid>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsReadOnly}" Value="false">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Border BorderThickness="1" BorderBrush="Red" CornerRadius="3" HorizontalAlignment="Left">
<AdornedElementPlaceholder x:Name="textBox"/>
</Border>
<Border Background="{DynamicResource Control.Validation.Background}" BorderBrush="{DynamicResource Control.Validation.Border}" BorderThickness="1" CornerRadius="3" >
<TextBlock Text="{Binding [0].ErrorContent}" Padding="3 0 3 0" Foreground="{DynamicResource Control.Validation.Foreground}" Margin="1"/>
<Border.Style>
<Style>
<Setter Property="Border.Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding AdornedElement.(DatePicker.IsFocused), ElementName=textBox}" Value="True">
<Setter Property="Border.Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我认为可以通过ElementName = PART_TextBox, Path=IsFocused
访问,但我总是得到Error
:找不到 PART_TextBox
答案 0 :(得分:0)
DatePicker还具有一个IsKeyboardFocusWithin
属性,如果焦点位于DatePicker的文本框中,则该属性为True。这可以解决我遇到的相同问题。
<Style TargetType="DatePicker">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>