我需要创建一个自定义下拉列表,其中包含一个用于打开它的按钮,以及一个用于显示内容的面板,我希望在单击它外部时关闭它,我应该使用哪种面板? 我尝试了popupmenu但是在lostfocus之后我无法关闭它,并尝试了contextmenu但是它点击它的内部项目就接近了。
任何建议都值得赞赏。
我试过这样的事情:
<Button x:Name="toggleButton"
ContextMenuService.Placement="Top" Click="ToggleButton_Click"
Margin="0" Content="+">
<Button.ContextMenu>
<ContextMenu Width="200"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch" >
<StackPanel>
<DataGrid >
<DataGrid.Columns>
<DataGridTextColumn/>
<DataGridTextColumn/>
</DataGrid.Columns>
</DataGrid>
<TextBlock Text="sadfasdfas" />
<TextBox Text="" Width="100"/>
</StackPanel>
</ContextMenu>
</Button.ContextMenu>
</Button>
答案 0 :(得分:2)
答案 1 :(得分:1)
最后我在2天后创造了我需要的东西!!,我真的无法找到更好的解决方案,我不得不放两个按钮,用于显示弹出窗口,另一个用于关闭弹出窗口!
in xaml
<ToggleButton x:Name="ShowPopup" Width="20" Height="20" Click="buttonShowPopup_Click" Panel.ZIndex="1" />
<ToggleButton x:Name="ClosePopup" Width="20" Height="20" Click="ClosePopupPopup_Click" />
<Popup x:Name="popup"
HorizontalOffset="0"
VerticalOffset="0"
PlacementTarget="{Binding ElementName=buttonShowPopup}"
Placement="Top"
PopupAnimation="Slide"
AllowsTransparency="True"
Focusable="True"
StaysOpen="False" Margin="36,0,-36,0">
<Grid LostFocus="Grid_LostFocus" >
<TextBox Text="asdasd"/>
</Grid>
</Popup>
在codebehind中
public DropDown()
{
InitializeComponent();
popup.Closed += popup_Closed;
}
private void ClosePopupPopup_Click(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
}
private void buttonShowPopup_Click(object sender, RoutedEventArgs e)
{
popup.IsOpen = true;
ClosePopup.SetValue(Canvas.ZIndexProperty, 1);
ShowPopup.SetValue(Canvas.ZIndexProperty, 0);
}
void popup_Closed(object sender, EventArgs e)
{
ShowPopup.SetValue(Canvas.ZIndexProperty, 1);
ClosePopup.SetValue(Canvas.ZIndexProperty, 0);
}