2013-07-30 1 views
1

저는 WPF 응용 프로그램을 만들고 있는데, 콤보 상자의 App.XAML에 정의 된 컨트롤 템플릿이 있습니다.WPF 컨트롤 템플릿이 렌더링 된 첫 번째 항목에 적용되지 않습니다.

응용 프로그램을 시작할 때 렌더링되는 첫 번째 콤보 상자는 컨트롤 템플릿에 정의 된 스타일을 사용하지 않지만 다른 모든 것은 수행합니다. 또한 첫 번째 콤보 상자에는 스타일이없는 첫 번째 항목이 있고 나머지 항목에는 해당 콤보 상자와 다른 항목의 스타일이 지정되어 있습니다.

무엇이 누락 되었습니까?

<Application x:Class="WpfPropertyGrid_Demo.App" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 

          <ResourceDictionary> 
           <!--Brushes defined here--> 
          </ResourceDictionary> 


          <ResourceDictionary> 
           <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton"> 
            <Grid> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition /> 
              <ColumnDefinition Width="20" /> 
             </Grid.ColumnDefinitions> 
             <Border 
              Grid.ColumnSpan="2" 
              Background="{StaticResource WindowBackgroundBrush}" /> 
              <Border 
               x:Name="Border" 
               Grid.Column="1" 
               Background="{StaticResource WindowBackgroundBrush}" /> 
               <Path 
                x:Name="Arrow" 
                Grid.Column="1"  
                Fill="{StaticResource GlyphBrush}" 
                HorizontalAlignment="Center" 
                VerticalAlignment="Center" 
                Data="M 0 0 L 4 4 L 8 0 Z"/> 
               </Grid> 
               <ControlTemplate.Triggers> 
                <Trigger Property="ToggleButton.IsMouseOver" Value="true"> 
                 <Setter TargetName="Border" Property="Background" Value="{StaticResource ActiveBrush}" /> 
                </Trigger> 
                <Trigger Property="ToggleButton.IsChecked" Value="true"> 
                 <Setter TargetName="Border" Property="Background" Value="{StaticResource ActiveBrush}" /> 
                 <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource ActiveGlyphBrush}" /> 
                </Trigger> 
                <Trigger Property="IsEnabled" Value="False"> 
                 <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" /> 
                 <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" /> 
                 <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/> 
                 <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource DisabledForegroundBrush}" /> 
                </Trigger> 
               </ControlTemplate.Triggers> 
              </ControlTemplate> 

              <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox"> 
               <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" /> 
              </ControlTemplate> 

              <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox"> 
               <Setter Property="SnapsToDevicePixels" Value="true"/> 
               <Setter Property="OverridesDefaultStyle" Value="true"/> 
               <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
               <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
               <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 
               <Setter Property="MinHeight" Value="20"/> 
               <Setter Property="Template"> 
                <Setter.Value> 
                 <ControlTemplate TargetType="ComboBox"> 
                  <Grid> 
                   <ToggleButton 
                    Name="ToggleButton" 
                    Template="{StaticResource ComboBoxToggleButton}" 
                    Grid.Column="2" 
                    Focusable="false" 
                    IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" 
                    ClickMode="Press"> 
                   </ToggleButton> 
                   <ContentPresenter 
                    Name="ContentSite" 
                    IsHitTestVisible="False" 
                    Content="{TemplateBinding SelectionBoxItem}" 
                    ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
                    ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
                    Margin="3,3,23,3" 
                    VerticalAlignment="Center" 
                    HorizontalAlignment="Left" /> 
                    <TextBox x:Name="PART_EditableTextBox" 
                     Style="{x:Null}" 
                     Template="{StaticResource ComboBoxTextBox}" 
                     HorizontalAlignment="Left" 
                     VerticalAlignment="Center" 
                     Margin="3,3,23,3" 
                     Focusable="True" 
                     Background="Transparent" 
                     Visibility="Hidden" 
                     IsReadOnly="{TemplateBinding IsReadOnly}"/> 
                     <Popup 
                      Name="Popup" 
                      Placement="Bottom" 
                      IsOpen="{TemplateBinding IsDropDownOpen}" 
                      AllowsTransparency="True" 
                      Focusable="False" 
                      PopupAnimation="Slide"> 
                      <Grid 
                       Name="DropDown" 
                       SnapsToDevicePixels="True"     
                       MinWidth="{TemplateBinding ActualWidth}" 
                       MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
                       <Border 
                        x:Name="DropDownBorder" 
                        Background="{StaticResource ActiveBrush}" /> 
                        <ScrollViewer Margin="4,4,4,4" 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="IsEnabled" Value="false"> 
                       <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/> 
                      </Trigger> 
                      <Trigger Property="IsGrouping" Value="true"> 
                       <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
                      </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> 
                  </Style.Triggers> 
                 </Style> 

                 <!-- SimpleStyles: ComboBoxItem --> 
                 <Style x:Key="{x:Type ComboBoxItem}" TargetType="ComboBoxItem"> 
                  <Setter Property="SnapsToDevicePixels" Value="true"/> 
                  <Setter Property="OverridesDefaultStyle" Value="true"/> 
                  <Setter Property="Template"> 
                   <Setter.Value> 
                    <ControlTemplate TargetType="ComboBoxItem"> 
                     <Border Name="Border" SnapsToDevicePixels="true"> 
                      <ContentPresenter /> 
                     </Border> 
                     <ControlTemplate.Triggers> 
                      <Trigger Property="IsHighlighted" Value="true"> 
                       <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/> 
                      </Trigger> 
                      <Trigger Property="IsEnabled" Value="false"> 
                       <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/> 
                      </Trigger> 
                      <Trigger Property="IsEnabled" Value="true"> 
                       <Setter Property="Foreground" Value="{StaticResource ActiveGlyphBrush}"/> 
                      </Trigger> 
                     </ControlTemplate.Triggers> 
                    </ControlTemplate> 
                   </Setter.Value> 
                  </Setter> 
                 </Style> 
                </ResourceDictionary> 

               </ResourceDictionary.MergedDictionaries> 
              </ResourceDictionary> 
             </Application.Resources> 
            </Application> 

여기에 실제 창 XAML입니다 : 여기

은 app.xaml의

<Window x:Class="WpfPropertyGrid_Demo.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:sys="clr-namespace:System;assembly=mscorlib" 
xmlns:wpg="clr-namespace:System.Windows.Controls" 
Title="WpfPropertyGrid Demo" mc:Ignorable="d" ResizeMode="CanResizeWithGrip" 
Width="360" Height="360" MinWidth="360" MinHeight="400"> 

<Window.Resources> 
    <ObjectDataProvider x:Key="SortTypes" MethodName="GetValues" ObjectType="{x:Type sys:Enum}"> 
     <ObjectDataProvider.MethodParameters> 
      <x:Type TypeName="wpg:PropertySort"/> 
     </ObjectDataProvider.MethodParameters> 
    </ObjectDataProvider> 
</Window.Resources> 

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 

    <StackPanel Orientation="Vertical" Margin="0,20,12,0" HorizontalAlignment="Right" VerticalAlignment="Top"> 
     <ComboBox Name="ComboSort" Margin="0,3,0,0" Width="95" FontSize="10" 
      SelectedIndex="0" ItemsSource="{Binding Source={StaticResource SortTypes}}" /> 
     <ComboBox Name="ComboSort2" Margin="0,3,0,0" Width="95" FontSize="10" 
      SelectedIndex="0" ItemsSource="{Binding Source={StaticResource SortTypes}}" /> 
    </StackPanel> 
</Grid> 

+1

일부 XAML을 표시 할 수 있습니까? – Nitesh

+0

미안하지만, 나는 내 마음을 잃었다. app.xaml이 지금은 – VARAK

+0

입니다. ComboBox 컨트롤의 나머지 부분에 적용되므로 ControlTemplate에 문제가있는 것처럼 보이지 않으므로 괜찮아 보입니다. ComboBox 컨트롤을 만들 위치를 XAML로 표시하십시오. – Nitesh

답변

2

는 별도의 ResourceDictionaryComboBox 스타일을 넣어 ComboBox.xaml 말을 주시기 바랍니다 및 이 ComboBox.xaml을 App.xaml에 병합하십시오.

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="ComboBox.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

나만의 템플릿을 만드는 모든 컨트롤에 대해 ResourceDictionary을 별도로 만들 것을 제안합니다. 이렇게하면 리소스를 쉽게 관리 할 수 ​​있습니다.

관련 문제