我有以下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>
我想将Content
Button的PART_Button
属性绑定到DatePicker本身的SelectedDate.Date
属性。我试过这样:
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DatePicker}}, Path=SelectedDate.Date}"
但它不起作用。按钮的内容是“显示日历”(我没有设置这个),如下图所示。
你对这个问题有什么建议吗?
答案 0 :(得分:1)
更改按钮以使用TextBlock作为其内容:
<Button x:Name="PART_Button">
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DatePicker}}, Path=SelectedDate.Date}"/>
</Button>