2016-09-01 8 views
2

私のUWPアプリケーションでは、ComboBoxを使用し、コンボボックスアイテムの上部と下部に配置されている余白を抑制しようとしています。UWPアプリケーションのComboBoxアイテムリストの上下に余白を設定する方法

ComboBox style templateを見ることによって、このマージンは、以下の値が操縦されているようです:0,7,0,7に設定されているComboBoxDropdownContentMargin、。

私はComboBoxの新しいスタイルを次のように作成しましたが、全く機能しません(私のComboBoxは消えます)。私は他の多くのソリューションを試しましたが、結果もありませんでした。

<Page.Resources> 
    <Style x:Key="ComboBoxStyle1" TargetType="ComboBox"> 
     <Setter Property="FontSize" Value="12"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Grid> 
         <Popup> 
          <Border> 
           <ScrollViewer> 
            <ItemsPresenter Margin="0,0,0,0"/> 
           </ScrollViewer> 
          </Border> 
         </Popup> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Page.Resources> 

私はUWP/XAMLコード化にはかなり新しいですので、私の質問は、ばかげているようだが、誰もが、私はテンプレートによって配置マージンを抑えることができますどのように私に言うことができる場合、私はお詫び申し上げます。

答えて

1

新しいスタイルを作成し、ItemsPresenterを変更することが正しいです。しかし、 'Template'セッターをオーバーライドすると、セッター全体が更新されます。上記のコードはItemsPresenter要素を残しましたが、他の要素はなくなり、コンボボックスを見ることはできません。このスタイルを完成させ、他のスタイルをデフォルトのスタイルと同じにしておけば、それはうまくいくでしょう。次のようなスタイルを更新してください:

<Style x:Key="ComboBoxStyle1" TargetType="ComboBox"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ComboBox"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="*" /> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*" /> 
          <ColumnDefinition Width="32" /> 
         </Grid.ColumnDefinitions> 
         <ContentPresenter 
          x:Name="HeaderContentPresenter" 
          Margin="{ThemeResource ComboBoxHeaderThemeMargin}" 
          FontWeight="{ThemeResource ComboBoxHeaderThemeFontWeight}" 
          x:DeferLoadStrategy="Lazy" 
          Content="{TemplateBinding Header}" 
          ContentTemplate="{TemplateBinding HeaderTemplate}" 
          FlowDirection="{TemplateBinding FlowDirection}" 
          Visibility="Collapsed" /> 
         <Border 
          x:Name="Background" 
          Grid.Row="1" 
          Grid.ColumnSpan="2" 
          Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" /> 
         <Border 
          x:Name="HighlightBackground" 
          Grid.Row="1" 
          Grid.ColumnSpan="2" 
          Background="{ThemeResource SystemControlHighlightListAccentLowBrush}" 
          BorderBrush="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          Opacity="0" /> 
         <ContentPresenter 
          x:Name="ContentPresenter" 
          Grid.Row="1" 
          Margin="{TemplateBinding Padding}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 
          <TextBlock 
           x:Name="PlaceholderTextBlock" 
           Foreground="{ThemeResource SystemControlPageTextBaseHighBrush}" 
           Text="{TemplateBinding PlaceholderText}" /> 
         </ContentPresenter> 
         <FontIcon 
          x:Name="DropDownGlyph" 
          Grid.Row="1" 
          Grid.Column="1" 
          Margin="0,10,10,10" 
          HorizontalAlignment="Right" 
          VerticalAlignment="Center" 
          Foreground="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
          FontFamily="{ThemeResource SymbolThemeFontFamily}" 
          FontSize="12" 
          AutomationProperties.AccessibilityView="Raw" 
          Glyph="&#xE0E5;" 
          IsHitTestVisible="False" /> 
         <Popup x:Name="Popup"> 
          <Border 
           x:Name="PopupBorder" 
           Margin="0,-1,0,-1" 
           HorizontalAlignment="Stretch" 
           Background="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}" 
           BorderBrush="{ThemeResource SystemControlForegroundChromeHighBrush}" 
           BorderThickness="{ThemeResource ComboBoxDropdownBorderThickness}"> 
           <ScrollViewer 
            x:Name="ScrollViewer" 
            MinWidth="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownContentMinWidth}" 
            Foreground="{ThemeResource SystemControlForegroundBaseHighBrush}" 
            AutomationProperties.AccessibilityView="Raw" 
            BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}" 
            HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" 
            HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" 
            IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" 
            IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" 
            IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" 
            VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" 
            VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" 
            VerticalSnapPointsAlignment="Near" 
            VerticalSnapPointsType="OptionalSingle" 
            ZoomMode="Disabled"> 
            <ItemsPresenter Margin="0"/> 
           </ScrollViewer> 
          </Border> 
         </Popup> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal" /> 
           <VisualState x:Name="PointerOver"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="Background"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageBackgroundAltMediumBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="BorderBrush"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="Background"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundListMediumBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="BorderBrush"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="Background"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="BorderBrush"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="HeaderContentPresenter" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextBlock" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DropDownGlyph" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="HighlightBackground" Storyboard.TargetProperty="BorderBrush"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <DoubleAnimation 
              Duration="0" 
              Storyboard.TargetName="HighlightBackground" 
              Storyboard.TargetProperty="Opacity" 
              To="1" /> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextBlock" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DropDownGlyph" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="FocusedPressed"> 
            <Storyboard> 
             <DoubleAnimation 
              Duration="0" 
              Storyboard.TargetName="HighlightBackground" 
              Storyboard.TargetProperty="Opacity" 
              To="1" /> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextBlock" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DropDownGlyph" Storyboard.TargetProperty="Foreground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unfocused" /> 
           <VisualState x:Name="PointerFocused" /> 
           <VisualState x:Name="FocusedDropDown"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames 
              Duration="0" 
              Storyboard.TargetName="PopupBorder" 
              Storyboard.TargetProperty="Visibility"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Visible</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="DropDownStates"> 
           <VisualState x:Name="Opened"> 
            <Storyboard> 
             <SplitOpenThemeAnimation 
              ClosedTargetName="ContentPresenter" 
              OffsetFromCenter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOffset}" 
              OpenedLength="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOpenedHeight}" 
              OpenedTargetName="PopupBorder" /> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Closed"> 
            <Storyboard> 
             <SplitCloseThemeAnimation 
              ClosedTargetName="ContentPresenter" 
              OffsetFromCenter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOffset}" 
              OpenedLength="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.DropDownOpenedHeight}" 
              OpenedTargetName="PopupBorder" /> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

あなたはまた、単にデフォルトのスタイルのコピーを編集することができ、XAMLデザイナはあなたのためのすべてのデフォルトスタイルを生成します、あなたはちょうどあなたが欲しいものを更新する必要があります。デフォルトのスタイルを生成する方法については、コンボボックスコントロール→右クリック→テンプレートの編集→コピーの編集を選択してください。screen shot

+0

ありがとうございました。 –

関連する問題