wpf组合框下拉列表显示在屏幕的左上角

时间:2015-05-08 11:41:10

标签: c# wpf combobox

我有一个带有自定义样式的组合框,但下拉项目显示在屏幕的左上角,为0,0。

<Style x:Key="CustomNetworkComboBox" TargetType="ComboBox">
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="MinWidth" Value="175"/>
        <Setter Property="Height" Value="22"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Visibility" Value="Hidden" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton Name="ToggleButton" Template="{StaticResource NetworkComboBoxToggleButton}" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
                        </ToggleButton>
                        <ContentPresenter Name="ContentSite" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                        <TextBox x:Name="PART_EditableTextBox" 
                                     Foreground="{StaticResource Black13}" Style="{x:Null}" Template="{StaticResource ComboBoxTextBox}"
                                     HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3,3,23,3"
                                     Focusable="True" Background="{x:Null}" Visibility="Hidden" IsReadOnly="True" BorderBrush="{x:Null}"/>
                        <Popup Name="PART_Popup"  IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide" Placement="Bottom" PlacementTarget="{Binding ElementName=ToggleButton}">
                            <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border x:Name="DropDownBorder" Background="{StaticResource Black88}" BorderThickness="1" CornerRadius="1" BorderBrush="{StaticResource Black67}"/>
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>

                        <Trigger Property="HasItems" Value="false">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger SourceName="PART_Popup" Property="Popup.AllowsTransparency" Value="true">
                            <Setter TargetName="DropDownBorder" Property="Margin" Value="0,-1,0,0"/>
                        </Trigger>
                        <Trigger Property="IsEditable" Value="true">
                            <Setter Property="IsTabStop" Value="false"/>
                            <Setter TargetName="PART_EditableTextBox" Property="Visibility"    Value="Visible"/>
                            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="True">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </Style.Triggers>
</Style>

我尝试了几种不同的解决方案&#34;谷歌搜索该问题的建议,但到目前为止还没有工作。

组合框位于网格内,该网格是列表视图中的项目,该视图通过VM填充数据。

<ComboBox Grid.Column="2" x:Name="Task" Margin="0,0,30,0" SelectedIndex="{Binding Path=IsCurrentComputer, Converter={StaticResource ComboboxIndexer}}" Style="{StaticResource CustomNetworkComboBox}"
                                TextBlock.FontFamily="/Views/Fonts/UniversLTStd-Ex.otf#Univers LT Std 53 Extended"
                                TextBlock.FontSize="12"
                                TextBlock.FontWeight="Normal"
                                TextBlock.Foreground="{StaticResource Black13}"
                                SelectedItem="{Binding Task}"
                              >

0 个答案:

没有答案