我花了數小時才弄清楚這個問題的答案,所以我想我會寫一個常見問題解答或回答我發現的問題。 (它是基於以下線程Binding Textbox IsFocused to Popup IsOpen plus additional conditions)WPF如何將Popup附加到簡單的UIElement,如矩形
我發現了很多綁定彈出窗口的東西,如切換按鈕和其他東西,基於Windows鉻和內置觸發器的例子。但在我的應用程序中,我想用自定義筆刷填充將彈出窗口綁定到一個簡單的矩形。我找不到一個關於如何在用戶將鼠標放在矩形上時打開彈出窗口並保持打開狀態的示例。
所以我發佈這個問題,我會立即發佈我找到的答案,以便希望其他人可以從中受益。我也會爲任何能夠幫助我理解stackoverflow是否允許這樣的帖子的人提供答案,或者我可以更好地解決這個問題。
EDIT 1)
我不能爲8小時,以便在這裏自我的答案是工作代碼: 下面是如何使用彈出一個基本的UIElement類似矩形一個簡單的例子/橢圓形的/ etc ...
<Grid HorizontalAlignment="Stretch" Height="Auto">
<Rectangle x:Name="PopupRec"
Grid.Row="0"
Width="20" Height="20"
HorizontalAlignment="Right"
Fill="Gray" Margin="0,0,0,10" />
<Popup x:Name="SortPopup"
PlacementTarget="{Binding ElementName=PopupRec}"
StaysOpen="False"
PopupAnimation="Slide"
AllowsTransparency="True">
<Border Background="White" Padding="15">
<StackPanel Orientation="Vertical">
<Button Command="{Binding MyCommand}" CommandParameter="5">5</Button>
<Button Command="{Binding MyCommand}" CommandParameter="10">10</Button>
<Button Command="{Binding MyCommand}" CommandParameter="15">15</Button>
<Button Command="{Binding MyCommand}" CommandParameter="20">20</Button>
</StackPanel>
</Border>
<Popup.Style>
<Style TargetType="{x:Type Popup}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=PopupRec, Path=IsMouseOver}" Value="True">
<Setter Property="IsOpen" Value="True" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=SortPopup, Path=IsMouseOver}" Value="True">
<Setter Property="IsOpen" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
</Popup>
</Grid>
只需粘貼這裏面的窗口/用戶控件的/ etc ...
太棒了,我完全忽略了那個相對的約束。它允許我將所有樣式移動到資源字典中並清理我的xaml。此外,謝謝你在frameworkelement的清晰度。這是我的第一個WPF項目,我有很多東西需要學習。 –