将按钮内容绑定到DatePicker SelectedDate

时间:2012-04-10 17:42:09

标签: wpf xaml styles datepicker controltemplate

我有以下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}"

但它不起作用。按钮的内容是“显示日历”(我没有设置这个),如下图所示。

enter image description here

你对这个问题有什么建议吗?

1 个答案:

答案 0 :(得分:1)

更改按钮以使用TextBlock作为其内容:

<Button x:Name="PART_Button">
    <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DatePicker}}, Path=SelectedDate.Date}"/>
</Button>