我有以下 DatePicker 控件:
<DatePicker SelectedDate="{Binding DateFor}" Style="{StaticResource ButtonDatePicker}"/>
ButtonDatePicker
样式定义如下:
<Style x:Key="ButtonDatePicker" TargetType="{x:Type DatePicker}">
<Setter Property="IsTodayHighlighted" Value="True"/>
<Setter Property="SelectedDateFormat" Value="Short"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DatePicker}">
<Grid>
<Button x:Name="PART_Button" Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DatePicker}}, Path=SelectedDate.Date}"/>
<Grid IsHitTestVisible="False" Opacity="0">
<Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" StaysOpen="False"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想将Button 的Content
属性绑定到DatePicker 本身的属性。我试过这样:PART_Button
SelectedDate.Date
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DatePicker}}, Path=SelectedDate.Date}"
但它不起作用。Button 的内容一直是“显示日历”(我没有设置),如下图所示。
你对这个问题有什么建议吗?