如何在图像上打开日历控件在WPF中动态单击

时间:2014-12-23 07:52:21

标签: wpf

我想在图像点击时填充日历控件并将该值绑定到wpf中的文本块。

 <Image Name="imgCalendar" Width="20" Height="30" Source="/Images/Cal.jpg" KeyDown="imgCalendar_KeyDown" />

<TextBlock Text="{Binding SelectedDate, ElementName=CalendarControl}"
           VerticalAlignment="Bottom"
           HorizontalAlignment="Left" />

<Calendar x:Name="CalendarControl"
          HorizontalAlignment="Center"
          VerticalAlignment="Center" 
        Visibility="Hidden" >
</Calendar>

1 个答案:

答案 0 :(得分:1)

这可以通过将图像包装到Toggle按钮并将Calencar包装到Popup中来完成。将 ToggleButton IsChecked 属性绑定到弹出 IsOpen 属性。

       <ToggleButton x:Name="btn">
            <Image Name="imgCalendar" Width="20" Height="30" Source="/Images/Cal.jpg"  />
        </ToggleButton>

        <TextBlock Text="{Binding SelectedDate, ElementName=CalendarControl}"
                   VerticalAlignment="Bottom"
                   HorizontalAlignment="Left" />
        <Popup IsOpen="{Binding ElementName=btn, Path=IsChecked}" StaysOpen="False">
            <Calendar x:Name="CalendarControl"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center">
            </Calendar>
        </Popup>