带有用户控制的弹出窗口,因为内容+在鼠标单击时自动关闭

本文关键字:单击 鼠标 控制 用户 因为 窗口 | 更新日期: 2023-09-27 18:30:45

我有一个由日历+时间选择器组成的WPF用户控件。我打算在弹出窗口中使用此用户控件,并在文本框焦点上显示弹出窗口。我的问题是我能够正确显示弹出窗口,但是当用户在日历中选择日期时,弹出窗口会自动关闭。

在选择特定日期之前,用户基本上没有滚动年/月/日的选项。如何保持弹出窗口打开,直到文本框失去焦点。

我试过StaysOpen + isOpen,但它们都不起作用。

谢谢

我正在发布 XAML 的一部分,该部分位于 texbox 的控件模板中

<Popup x:Name="DatePickerPopup" IsOpen="False"
                           Width="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}"
                           Height="{TemplateBinding Height}">
                        <Grid>
                            <Calendar/>
                        </Grid>
                    </Popup>

在我的自定义控件中有布尔依赖项属性后

<Popup x:Name="DatePickerPopup" IsOpen="{Binding IsPopupOpen, RelativeSource={RelativeSource TemplatedParent}}"
                           Width="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}"
                           Height="{TemplateBinding Height}">
                        <Grid>
                            <Calendar/>
                        </Grid>
                    </Popup>

带有用户控制的弹出窗口,因为内容+在鼠标单击时自动关闭

我在几个Popup控件中具有类似的功能,IsOpen非常适合我......我不确定你在用它做什么。我将其绑定到bool属性,并在希望它打开或关闭时更改此属性值:

<Popup Name="SuggestionsPopup" IsOpen="{Binding IsPopupOpen, RelativeSource={
    RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:
    AutoCompleteTextBox}}}" StaysOpen="False" MaxHeight="{Binding MaxPopupHeight, 
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Controls:
    AutoCompleteTextBox}}}" AllowsTransparency="True">

我已经解决了这个问题。事实上,我下载了WPF工具包的源代码,可在 http://wpftoolkit.codeplex.com/获得。

他们有一个具有相同方案的日期时间选取器控件。

我添加了一个切换按钮,然后将弹出窗口的 IsOpen 属性绑定到切换按钮的 IsChecked 属性。事情对我来说很好。这是演示代码。

<ToggleButton Margin="1" x:Name="_calenderButton"
                        IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}}"
                        IsEnabled="{Binding IsDateTimePickerReadOnlyCallback, 
                        RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource invertBoolConverter}}"
                        Grid.Column="1">
                        <Image Height="20" Width="20" Source="pack://application:,,,/Common;component/Images/calender2.jpg"/>
                    </ToggleButton>

<Popup x:Name="PART_Popup" IsOpen="{Binding IsChecked, ElementName=_calenderButton}" StaysOpen="False">
                        <Border BorderThickness="1" Padding="3" Background="{StaticResource PopupBackgroundBrush}" 
                                BorderBrush="{StaticResource PopupDarkBorderBrush}">
                            <StackPanel>
                                <Calendar x:Name="PART_Calender" BorderThickness="0"/>
                                <commonControls:TimePicker x:Name="PART_TimePicker"
                                                           />
                            </StackPanel>
                        </Border>
                    </Popup>

干杯