C# 使用现有零件重新设置控件模板的样式

C# 使用现有零件重新设置控件模板的样式,c#,wpf,xaml,wpf-controls,wpf-style,C#,Wpf,Xaml,Wpf Controls,Wpf Style,我正在尝试从中重新设置DateTimePicker的控件模板的样式。下面是它应该是什么样子的图片: 以下是原始代码的相关部分: 注意:这不是我唯一改变的事情;实际上,我省略了另外两种样式(MetroTimePicker和MetroButtonSpinner)。大多数情况下,一切看起来都很好,但它的行为并不正确。每当我在代码中设置断点时(调用ApplyTemplate之后),我都可以看到private\u calendar字段为空。直接调用myPicker.GetTemplateChild(

我正在尝试从中重新设置
DateTimePicker
的控件模板的样式。下面是它应该是什么样子的图片:

以下是原始代码的相关部分:

注意:这不是我唯一改变的事情;实际上,我省略了另外两种样式(
MetroTimePicker
MetroButtonSpinner
)。大多数情况下,一切看起来都很好,但它的行为并不正确。每当我在代码中设置断点时(调用
ApplyTemplate
之后),我都可以看到private
\u calendar
字段为空。直接调用myPicker.GetTemplateChild(“PART_Calendar”)也会返回null(这在监视或即时窗口中是可能的)


当我应用自定义样式时,它似乎不再能够拾取模板中的命名元素。我肯定遗漏了什么,因为我认为我可以应用几乎所有的控件模板,只要所有命名的部分都在那里(并且具有适当的类型)。所以我的问题是,如何将自定义模板应用于WPF控件,并确保与其命名部分相关的任何逻辑继续按预期工作?

好吧,我是个白痴。我不知道我看这个有多久了,每一个名字都要反复检查,但不知怎么的,我从来没有看到过这个愚蠢的小错误:

<Calendar x:Name="Part_Calendar" … />

应该是:

<Calendar x:Name="PART_Calendar" … />

这是一个非常特殊的临时场景。除非你有一些特定的异常信息/错误要分享,否则你需要仔细研究这件事,这可能有助于整个演绎推理顺序的故障排除?@ChrisW。是的,对不起,我为这个问题绞尽脑汁已经有一段时间了。完整的样式非常大(实际上包括3种不同的样式),所以我不想在这里过多地涉及到这一点。然而,我认为核心问题是
\u calendar
字段为空,考虑到我风格的整体结构实际上与原始风格非常相似,我想不出会发生这种情况的任何原因。没有例外……你确定你用了所有的零件?看到一切都可能有帮助,打赌这是覆盖控件模板的一些细微差别。@ChrisW。我已经为
DateTimePicker
添加了整个样式。希望能有帮助。
<Style x:Key="MetroDateTimePicker" TargetType="{x:Type xctk:DateTimePicker}">
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}"/>
    <Setter Property="Background" Value="{DynamicResource ControlBackgroundBrush}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="{DynamicResource TextBoxBorderBrush}"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="FontFamily" Value="{DynamicResource ContentFontFamily}"/>
    <Setter Property="FontSize" Value="{DynamicResource ContentFontSize}"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type xctk:DateTimePicker}">
                <Grid>
                    <Border x:Name="Base"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    <Grid Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <xctk:ButtonSpinner x:Name="PART_Spinner"
                                            Grid.Column="0"
                                            BorderThickness="0"
                                            IsTabStop="False"
                                            Background="Transparent"
                                            Style="{StaticResource MetroButtonSpinner}"
                                            AllowSpin="{TemplateBinding AllowSpin}"
                                            ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}">
                            <xctk:WatermarkTextBox x:Name="PART_TextBox"
                                                   BorderThickness="0" 
                                                   Background="Transparent"
                                                   FontFamily="{TemplateBinding FontFamily}"
                                                   FontSize="{TemplateBinding FontSize}"
                                                   FontStretch="{TemplateBinding FontStretch}"
                                                   FontStyle="{TemplateBinding FontStyle}"
                                                   FontWeight="{TemplateBinding FontWeight}"
                                                   Foreground="{TemplateBinding Foreground}"
                                                   HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                   IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"
                                                   MinWidth="20"
                                                   AcceptsReturn="False"
                                                   Padding="0"
                                                   TextAlignment="{TemplateBinding TextAlignment}"
                                                   TextWrapping="NoWrap" 
                                                   Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"
                                                   TabIndex="{TemplateBinding TabIndex}"
                                                   Watermark="{TemplateBinding Watermark}"
                                                   WatermarkTemplate="{TemplateBinding WatermarkTemplate}" />
                        </xctk:ButtonSpinner>
                        <ToggleButton x:Name="_calendarToggleButton"
                                      Background="{TemplateBinding Background}"
                                      Grid.Column="1"
                                      IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}}"
                                      Style="{DynamicResource ChromelessButtonStyle}"
                                      Foreground="{TemplateBinding Foreground}"
                                      IsTabStop="False">
                            <Path Fill="{TemplateBinding Foreground}"
                                  Data="..."
                                  Stretch="Uniform">
                                <Path.Width>
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                             Path="FontSize"
                                             Converter="{x:Static shared:FontSizeOffsetConverter.Instance}">
                                        <Binding.ConverterParameter>
                                            <sys:Double>4</sys:Double>
                                        </Binding.ConverterParameter>
                                    </Binding>
                                </Path.Width>
                                <Path.Height>
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}" 
                                             Path="FontSize" 
                                             Converter="{x:Static shared:FontSizeOffsetConverter.Instance}">
                                        <Binding.ConverterParameter>
                                            <sys:Double>4</sys:Double>
                                        </Binding.ConverterParameter>
                                    </Binding>
                                </Path.Height>
                            </Path>
                        </ToggleButton>
                    </Grid>
                    <Popup x:Name="PART_Popup" 
                           AllowsTransparency="True"
                           IsOpen="{Binding IsChecked, ElementName=_calendarToggleButton}"
                           PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                           StaysOpen="False">
                        <Border Padding="3"
                                Background="{DynamicResource WhiteBrush}"
                                BorderBrush="{DynamicResource ComboBoxPopupBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Effect="{DynamicResource DropShadowBrush}">
                            <StackPanel>
                                <Calendar x:Name="Part_Calendar"
                                          BorderThickness="0"
                                          MinWidth="115"
                                          DisplayDateStart="{Binding Minimum, RelativeSource={RelativeSource TemplatedParent}}"
                                          DisplayDateEnd="{Binding Maximum, RelativeSource={RelativeSource TemplatedParent}}"
                                          IsTodayHighlighted="False"/>
                                <xctk:TimePicker x:Name="PART_TimeUpDown"
                                                 Style="{StaticResource MetroTimePicker}"
                                                 Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
                                                 Foreground="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" 
                                                 Format="{TemplateBinding TimeFormat}"
                                                 FormatString="{TemplateBinding TimeFormatString}"
                                                 Value="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}"
                                                 Minimum="{Binding Minimum, RelativeSource={RelativeSource TemplatedParent}}"
                                                 Maximum="{Binding Maximum, RelativeSource={RelativeSource TemplatedParent}}"
                                                 ClipValueToMinMax="{Binding ClipValueToMinMax, RelativeSource={RelativeSource TemplatedParent}}"
                                                 IsUndoEnabled="{Binding IsUndoEnabled, RelativeSource={RelativeSource TemplatedParent}}"
                                                 AllowSpin="{TemplateBinding TimePickerAllowSpin}"
                                                 ShowButtonSpinner="{TemplateBinding TimePickerShowButtonSpinner}"
                                                 Watermark="{TemplateBinding TimeWatermark}"
                                                 WatermarkTemplate="{TemplateBinding TimeWatermarkTemplate}"
                                                 Visibility="{TemplateBinding TimePickerVisibility}"
                                                 Margin="3 0 3 3"/>
                            </StackPanel>
                        </Border>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="False" />
                            <Condition Binding="{Binding AllowTextInput, RelativeSource={RelativeSource Self}}" Value="False" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="IsReadOnly" Value="True" TargetName="PART_TextBox" />
                    </MultiDataTrigger>
                    <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True">
                        <Setter Property="IsReadOnly" Value="True" TargetName="PART_TextBox" />
                    </DataTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Calendar x:Name="Part_Calendar" … />
<Calendar x:Name="PART_Calendar" … />