ToggleButton
を開くためのがあり、既知のすべての要素などPopup
と同じ動作をするとします。ComboBox
...これはこのコードです:
<ToggleButton x:Name="PART_OpenToggleButton"
Focusable="False"
IsChecked="False"
Template="{StaticResource MyToggleButton}">
<Grid>
<Popup x:Name="PART_PopupControl"
Style="{StaticResource MyPopupStyle}"
StaysOpen="False"
VerticalAlignment="Bottom"
IsOpen="False"
PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToggleButton, AncestorLevel=1}}" />
</Grid>
</ToggleButton>
次に、コード ビハインドで . IsOpen
とPopup
. IsChecked
のためにToggleButton
。すべてが機能しますが、 を開いPopup
て境界線の外側をクリックすると問題が発生します。はPopup
閉鎖されますが、ToggleButton
チェックされたままです。
をクリックしてを閉じると、 はそれ自体を閉じて設定しますが、同時に をクリックすると、を再度開こうとするPopupOnClosed
ため、ハンドラで を設定することはできません。したがって、閉じることはできません。ToggleButton.IsChecked = false
ToggleButton
Popup
Popup
ToggleButton.IsChecked = false
ToggleButton
Popup
最初のトグルボタンクリック:
-> ToggleButton IsChecked = true
2 番目のトグル ボタン クリック:
-> ToggleButton IsChecked = false
-> ToggleButton IsChecked = true
そのため、ポップアップが開いているときにトグル ボタンをクリックすると、ポップアップは点滅しますが、開いたままになります。
この問題をどのように解決しますか?
編集:
これを MyWindow.XAML で試して、コード ビハインドに依存関係プロパティ IsDropDownOpen を追加してください。
<Grid>
<ToggleButton x:Name="PART_OpenToggleButton"
Focusable="False"
Height="20"
Width="50"
IsChecked="{Binding ElementName=TestWindow, Mode=TwoWay, Path=IsDropDownOpen}">
<Grid>
<Popup x:Name="PART_PopupControl"
Width="100"
Height="100"
StaysOpen="False"
Focusable="False"
VerticalAlignment="Bottom"
IsOpen="{Binding ElementName=TestWindow, Path=IsDropDownOpen}"
PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToggleButton, AncestorLevel=1}}">
</Popup>
</Grid>
</ToggleButton>
</Grid>
public bool IsDropDownOpen
{
get { return (bool)GetValue(IsDropDownOpenProperty); }
set { SetValue(IsDropDownOpenProperty, value); }
}
public static readonly DependencyProperty IsDropDownOpenProperty =
DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(Window), new UIPropertyMetadata(false));