我一直在修补这个问题,似乎无法找到问题所在。我的造型很完美,但组合框没有在主条中显示所选项目(正如您在我嵌入的图像中看到的那样)。下拉列表文本是可见的,但它不显示所选项目。
我对XAML的了解有限,所以我希望有人能发现我所犯的错误。
<Window.Resources>
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#3a3a3a" />
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Border
x:Name="Border"
Grid.ColumnSpan="2"
Background="{StaticResource WindowBackgroundBrush}"
BorderBrush="#4a4a4a"
BorderThickness="1" />
<Border
Grid.Column="0"
Margin="1"
Background="{StaticResource WindowBackgroundBrush}" />
<Path
x:Name="Arrow"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"
Fill="White" />
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<Border x:Name="PART_ContentHost" Focusable="False" />
</ControlTemplate>
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton
Name="ToggleButton"
Grid.Column="2"
ClickMode="Press"
Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Template="{StaticResource ComboBoxToggleButton}" />
<ContentPresenter
Name="ContentSite"
Margin="3,3,23,3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
IsHitTestVisible="False" />
<TextBox
x:Name="PART_EditableTextBox"
Margin="3,3,23,3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Focusable="True"
IsReadOnly="{TemplateBinding IsReadOnly}"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
Visibility="Hidden" />
<Popup
Name="Popup"
AllowsTransparency="True"
Focusable="False"
IsOpen="{TemplateBinding IsDropDownOpen}"
Placement="Bottom"
PopupAnimation="Slide">
<Grid
Name="DropDown"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}"
SnapsToDevicePixels="True">
<Border
x:Name="DropDownBorder"
Background="{StaticResource WindowBackgroundBrush}"
BorderThickness="1" />
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers />
</Style>
</Window.Resources>
<ComboBox
Name="cbxEmployees"
Width="Auto"
Margin="10,0,0,0"
VerticalContentAlignment="Center"
Foreground="White"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Employees}"
SelectedItem="{Binding SelectedEmployee}"
SelectionChanged="cbxEmployees_SelectionChanged" />
答案 0 :(得分:0)
尝试将ContentTemplate绑定到模板的SelectionBoxItem,如下所示:
<ContentPresenter
Name="ContentSite"
Margin="3,3,23,3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}" />
SelectionBoxItem保存要在Combobox上显示的对象。 如果你覆盖控件的默认模板而没有在某处绑定那个prop,它就会丢失。