我有一个弹出控件,里面有一个按钮。组合框打开时应显示弹出窗口,关闭组合框时应弹出。 我的代码如下所示:
<Popup Name="myPopup" IsOpen="{Binding Path=IsDropDownOpen, ElementName=combo}"
Placement="Right" PlacementTarget="{Binding ElementName=combo}"
StaysOpen="False" Focusable="False"
AllowsTransparency="True" PopupAnimation="Fade" >
<Button Click="Popup_Click">
<Image Source="..\Images\edit.png" Height="30"/>
</Button>
</Popup>
<ComboBox Name="combo" SelectedValuePath="Key" DisplayMemberPath="Value"
Loaded="ComboBox_Loaded" SelectedValue="{Binding FamilyStatus}">
</ComboBox>
功能:
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
combo.ItemsSource = GetComboValues();
}
在这段代码中我遇到了一个问题: 当组合框打开时显示弹出窗口,当组合关闭但是点击按钮(来自弹出窗口内)未被触发时关闭弹出窗口!我想这是因为当鼠标移动到按钮时,弹出窗口的“IsOpen”设置为false,因此点击不再被触发。 我尝试了一种不同的方法来设置IsOpen =“False”并用组合框事件打开弹出窗口:
DropDownOpened =“ComboBox_Loaded_DropDownOpened”和StaysOpen =“False”
private void ComboBox_Loaded_DropDownOpened(object sender, EventArgs e)
{
myPopup.IsOpen = true;
myPopup.StaysOpen = false;
}
但是,即使我在窗口上选择了不同的文本框,弹出窗口也会保持打开状态,并且只有在两次单击后才会引发按钮的单击事件。 (如果我添加DropDownClosed="ComboBox_Loaded_DropDownClosed"
事件来关闭弹出窗口,弹出窗口再次消失,然后我可以按下按钮)。
有人可以帮忙解决问题吗?
答案 0 :(得分:0)
你应该重新模仿你的组合框,所以属性IsDropDownOpen不会再影响它,然后你可以强迫组合保持开放,即使他失去了焦点。我使用属性Tag替换IsDropDownOpen,并在两个弹出窗口中设置StaysOpen = true(也在comboBox模板中)
这是代码:
组合框模板:
<Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="MainGrid" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup" StaysOpen="True" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
<Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
<Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
<ScrollViewer x:Name="DropDownScrollViewer">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</ScrollViewer>
</Border>
</Microsoft_Windows_Themes:SystemDropShadowChrome>
</Popup>
<ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding Tag, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="ClickMode" Value="Press"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true">
<Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
<Path x:Name="Arrow" Data="M 0 0 L 3.5 4 L 7 0 Z" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/>
</Grid>
</Microsoft_Windows_Themes:ButtonChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
<ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
<Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
<Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
<Setter Property="Background" Value="#FFF4F4F4"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
<Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
<Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您的代码:
<Popup x:Name="myPopup" IsOpen="{Binding Tag, ElementName=combo}"
Placement="Right" PlacementTarget="{Binding ElementName=combo}"
StaysOpen="True" Focusable="False"
AllowsTransparency="True" PopupAnimation="Fade" >
<Button Click="Popup_Click">
<Image Source="..\Images\edit.png" Height="30"/>
</Button>
</Popup>
<ComboBox Name="combo" SelectedValuePath="Key" DisplayMemberPath="Value"
Loaded="ComboBox_Loaded" SelectedValue="{Binding FamilyStatus}" Style="{StaticResource ComboBoxStyle1}">
</ComboBox>