2010-11-10 13 views
0

我试图按照以下方式设置我的组合框的样式。WPF:Combobox自定义ContentTemplate将namespace.classname显示为选定的项目文本

下拉列表显示正确的名称列表,但选中时,namespace.classname显示为组合文本而不是所选名称。

我有以下风格和组合框声明。任何人都知道我错了什么?谢谢!

<!--ComboBox Style--> 
<Style TargetType="ComboBox" x:Key="DefaultComboBoxStyle"> 
    <Setter Property="MinHeight" Value="26" /> 
    <Setter Property="Padding" Value="6,4,25,4"/> 
    <Setter Property="Foreground" Value="{StaticResource TextBrush}"/> 
    <Setter Property="FontFamily" Value="{StaticResource ContentFontFamily}"/> 
    <Setter Property="FontSize" Value="{StaticResource ContentFontSize}"/> 
    <Setter Property="TextOptions.TextHintingMode" Value="Animated"/> 
    <Setter Property="Background" Value="{StaticResource TransparentLightestBrush}" />    
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="BorderThickness" Value="1"/>   
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorderBrush}"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ComboBox"> 
       <Grid> 
        <Grid.Resources> 
         <Style x:Key="comboToggleStyle" TargetType="ToggleButton"> 
          <Setter Property="Foreground" Value="{StaticResource TextBrush}"/> 
          <Setter Property="Background" Value="{StaticResource GrayBrush7}"/> 
          <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorderBrush}"/> 
          <Setter Property="BorderThickness" Value="1"/> 
          <Setter Property="Padding" Value="3"/> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="ToggleButton"> 
             <Grid> 
              <VisualStateManager.VisualStateGroups> 
               <VisualStateGroup x:Name="CommonStates"> 
                <VisualState x:Name="Normal"/> 
                <VisualState x:Name="MouseOver"> 
                 <Storyboard> 
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="MouseOverBorder"> 
                   <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
                  </DoubleAnimationUsingKeyFrames> 
                 </Storyboard> 
                </VisualState> 
                <VisualState x:Name="Pressed"> 
                 <Storyboard> 
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PressedBorder"> 
                   <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
                  </DoubleAnimationUsingKeyFrames> 
                 </Storyboard> 
                </VisualState> 
                <VisualState x:Name="Disabled"/> 
               </VisualStateGroup> 
               <VisualStateGroup x:Name="CheckStates"> 
                <VisualState x:Name="Checked"> 
                 <Storyboard> 
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CheckedRectangle"> 
                   <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="1"/> 
                  </DoubleAnimationUsingKeyFrames> 
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CheckedInnerRectangle"> 
                   <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="1"/> 
                  </DoubleAnimationUsingKeyFrames> 
                 </Storyboard> 
                </VisualState> 
                <VisualState x:Name="Unchecked"/> 
               </VisualStateGroup> 
               <VisualStateGroup x:Name="FocusStates"> 
                <VisualState x:Name="Focused"> 
                 <Storyboard> 
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="FocusRectangle"> 
                   <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
                  </DoubleAnimationUsingKeyFrames> 
                  <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="FocusInnerRectangle"> 
                   <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
                  </DoubleAnimationUsingKeyFrames> 
                 </Storyboard> 
                </VisualState> 
                <VisualState x:Name="Unfocused"/> 
               </VisualStateGroup> 
              </VisualStateManager.VisualStateGroups> 
              <Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3"/> 
              <Rectangle x:Name="DisabledVisualElement" Fill="{StaticResource WhiteBrush}" IsHitTestVisible="false" Opacity="0" RadiusY="3" RadiusX="3"/> 
              <Border x:Name="MouseOverBorder" Background="{StaticResource TransparentDarkBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3.5" Opacity="0"/> 
              <Border x:Name="PressedBorder" Background="{StaticResource GrayBrush5}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3.5" Opacity="0"/> 
              <Rectangle x:Name="FocusRectangle" Stroke="{StaticResource TextBoxMouseOverInnerBorderBrush}" RadiusY="4" RadiusX="4" Margin="-1" Opacity="0" /> 
              <Rectangle x:Name="FocusInnerRectangle" StrokeThickness="{TemplateBinding BorderThickness}" Stroke="{StaticResource TextBoxMouseOverBorderBrush}" RadiusX="3" RadiusY="3" Opacity="0" /> 
              <Rectangle x:Name="CheckedRectangle" StrokeThickness="{TemplateBinding BorderThickness}" RadiusY="4" RadiusX="4" Opacity="0" Stroke="{StaticResource TextBoxMouseOverBorderBrush}" /> 
              <Rectangle x:Name="CheckedInnerRectangle" StrokeThickness="{TemplateBinding BorderThickness}" RadiusY="4" RadiusX="4" Opacity="0" Margin="1" Stroke="{StaticResource TextBoxMouseOverInnerBorderBrush}" /> 
              <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
              <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" RadiusY="3.5" RadiusX="3.5" Stroke="{StaticResource HighlightBrush}" StrokeThickness="1" Visibility="Collapsed"/> 
             </Grid> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </Grid.Resources> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"/> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="DisabledVisualElement"> 
             <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.45"/> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="FocusRectangle"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
            </DoubleAnimationUsingKeyFrames> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="FocusInnerRectangle"> 
             <EasingDoubleKeyFrame KeyTime="0" Value="1"/> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unfocused"/> 
          <VisualState x:Name="FocusedDropDown"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Duration="00:00:00" Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="PopupBorder"> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="ValidationStates"> 
          <VisualState x:Name="Valid"/> 
          <VisualState x:Name="InvalidUnfocused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="InvalidFocused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <sys:Boolean>True</sys:Boolean> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border x:Name="ContentPresenterBorder" Background="{TemplateBinding Background}"> 
         <Grid> 
          <ContentPresenter Content="{TemplateBinding SelectionBoxItem}" 
           ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
           ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" 
           Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
          <ToggleButton x:Name="DropDownToggle" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
              BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" Margin="0" Style="{StaticResource comboToggleStyle}" VerticalAlignment="Stretch"> 
           <Path x:Name="BtnArrow" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z " HorizontalAlignment="Right" Height="4" Margin="0,0,6,0" Stretch="Uniform" Width="8"> 
            <Path.Fill> 
             <SolidColorBrush x:Name="BtnArrowColor" Color="{StaticResource Gray3}"/> 
            </Path.Fill> 
           </Path> 
          </ToggleButton> 
         </Grid> 
        </Border> 
        <Rectangle x:Name="FocusRectangle" Stroke="{StaticResource TextBoxMouseOverInnerBorderBrush}" RadiusY="4" RadiusX="4" Margin="-1" Opacity="0" /> 
        <Rectangle x:Name="FocusInnerRectangle" StrokeThickness="{TemplateBinding BorderThickness}" Stroke="{StaticResource TextBoxMouseOverBorderBrush}" RadiusX="3" RadiusY="3" Opacity="0" /> 
        <Rectangle x:Name="DisabledVisualElement" Fill="{StaticResource DisabledWhiteColorBrush}" IsHitTestVisible="false" RadiusX="3" RadiusY="3" Opacity="0"/>       
        <Popup x:Name="PART_Popup" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" 
          AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
         <Border Effect="{StaticResource DropShadowBrush}" x:Name="PopupBorder" BorderThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="Stretch" Height="Auto" BorderBrush="{StaticResource ComboBoxPopupBrush}" Background="{StaticResource WhiteBrush}"> 
          <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1"> 
           <ItemsPresenter/> 
          </ScrollViewer> 
         </Border> 
        </Popup> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelectionBoxHighlighted" Value="True"/> 
          <Condition Property="IsDropDownOpen" Value="False"/> 
         </MultiTrigger.Conditions> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
        </MultiTrigger> 
        <Trigger Property="HasItems" Value="False"> 
         <Setter Property="MinHeight" TargetName="PopupBorder" Value="95"/> 
        </Trigger> 
        <Trigger Property="IsGrouping" Value="True"> 
         <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

编辑:

感谢伊恩。

这是我使用的数据。只需将列表分配给组合框项目源即可。选择任何名称时,不是显示“Eric”或“Stan”或“Kyle”,而是始终显示“WpfCosmo.Person”。

在我ControlsPage.xaml:

<ComboBox Height="22" Margin="0,0,0,5" x:Name="comboBox" DisplayMemberPath="Name" SelectedIndex="-1" /> 

在背后说我的代码:

public partial class ControlsPage : Page 
{ 
    public ControlsPage() 
    { 
     InitializeComponent(); 

     comboBox.ItemsSource = Person.GetList(); 
    } 
} 

public class Person 
{ 
    public string Name { get; set; } 
    public string Surname { get; set; } 

    public static List<Person> GetList() 
    { 
     List<Person> SouthPark = new List<Person>() { 
      new Person() { Name = "Eric", Surname="Cartman" }, 
      new Person() { Name = "Stan", Surname="Marsh" }, 
      new Person() { Name = "Kyle", Surname="Broflovski" }, 
      new Person() { Name = "Kenny", Surname="McCormick" }, 
      new Person() { Name = "Bebe", Surname="Stevens" }, 
      new Person() { Name = "Clyde", Surname="Donovan" }, 
      new Person() { Name = "Craig", Surname="Tucker" }, 
      new Person() { Name = "Jimmy", Surname="Vulmer" }, 
      new Person() { Name = "Pip", Surname="Pirrup" }, 
      new Person() { Name = "Token", Surname="Black" }, 
      new Person() { Name = "Tweek", Surname="Tweak" }, 
      new Person() { Name = "Wendy", Surname="Testaburger" }, 
      new Person() { Name = "Annie", Surname="Polk" }, 
      new Person() { Name = "Randy", Surname="Marsh" }, 
      new Person() { Name = "Sharon", Surname="Marsh" }, 
      new Person() { Name = "Shelley", Surname="Marsh" }, 
      new Person() { Name = "Marvin", Surname="Marsh" }, 
      new Person() { Name = "Jimbo", Surname="Kern" }, 
      new Person() { Name = "Gerald", Surname="Broflovski" }, 
      new Person() { Name = "Sheila", Surname="Broflovski" }, 
      new Person() { Name = "Ike", Surname="Broflovski" }, 
      new Person() { Name = "Kyle", Surname="Schwartz" }, 
      new Person() { Name = "Liane", Surname="Cartman" }, 
      new Person() { Name = "Stuart", Surname="McCormick" }, 
      new Person() { Name = "Carol", Surname="McCormick" }, 
      new Person() { Name = "Kevin", Surname="McCormick" }, 
      new Person() { Name = "Stephen", Surname="Stotch" }, 
      new Person() { Name = "Linda", Surname="Stotch" }, 
      new Person() { Name = "Richard", Surname="Tweak" } 
     }; 

     return SouthPark; 
    } 
} 
+0

我刚刚粘贴了你的例子,我不能重现这个问题。我使用DataType设置为我的自定义数据类型的DataTemplate,并且该模板在TextBlock中显示名为“Value”的属性。这里您的自定义模板正确地将该模板应用于组合文本区域中。所以我在两个地方看到了Value属性 - 而不是namespace.classname。所以这个问题必须具体到你如何使用ComboBox--我们需要更多的信息。 – 2010-11-10 08:55:54

+0

对于它的价值,我也尝试不使用DataTemplate - 我只是将DisplayMemberPath设置为在我的测试中引用Value属性。再次,它与你的模板很好地工作 - 我不能重现你描述的问题。您能否提供更多信息 - 您如何使用ComboBox?特别是,你说你想看到“正确的名称列表” - 什么决定了“正确”在这里? (WPF的默认行为是在某些情况下调用ToString,所以要看看namespace.classname以外的东西,你需要告诉WPF你想要什么,你告诉它什么?) – 2010-11-10 08:58:43

+0

任何帮助?不希望使用数据模板〜:( – Misaki 2010-11-18 05:04:05

回答

1

尝试删除样式的这一部分:

Content="{TemplateBinding SelectionBoxItem}" 
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
0

我知道这个话题老,但我最近有同样的问题。我想为我的组合框定制样式,因此我开始在Visual Studio中提取默认模板以对其进行修改。但是当我这样做时,我的组合框不再工作了。

在谷歌的一些研究之后,我发现这里一个解决方案:StackOverFlow : Custom Template for ComboBox ignores use of DisplayMemberPath

的问题来自于Visual Studio的模板提取(我相信它是混合同样的事情),它忘在内容呈现一个属性:

ContentTemplateSelector = “{TemplateBinding ItemTemplateSelector}”

这个简单的财产已经修复了这个问题对我来说。

希望这可以帮助别人。

相关问题