我正在尝试如下设置我的组合框样式。
下拉列表显示正确的名称列表,但选中后,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 :(得分:1)
尝试删除这部分风格:
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
答案 1 :(得分:0)
我知道这个话题很老,但最近我遇到了同样的问题。我想为我的组合框设置一个自定义样式,所以我开始在Visual Studio中提取默认模板来修改它。但是当我这样做时,我的组合框架不再起作用了。
经过对Google的一些研究后,我在这里找到了一个解决方案:StackOverFlow : Custom Template for ComboBox ignores use of DisplayMemberPath
问题来自Visual Studio模板提取器(我相信它与Blend的相同之处)忘记了Content Presenter中的属性:
ContentTemplateSelector =“{TemplateBinding ItemTemplateSelector}”
这个简单的属性为我解决了这个问题。
希望这可以帮助别人。