我有简单的组合框模板,如下所示:
<SolidColorBrush x:Key="NormalForegroundBrush" Color="Black" />
<SolidColorBrush x:Key="GlyphBrush" Color="#4c4c4c" />
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}" >
<Setter Property="TextElement.Foreground" Value="{StaticResource NormalForegroundBrush}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<ToggleButton ClickMode="Press" x:Name="ToggleButton" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Focusable="False" Grid.Column="2" />
<ContentPresenter Margin="3,3,23,3" HorizontalAlignment="Left" x:Name="ContentSite" VerticalAlignment="Center" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
Content="{TemplateBinding SelectionBoxItem}" IsHitTestVisible="False" />
<TextBox Margin="3,3,23,3" Visibility="Hidden" HorizontalAlignment="Left" x:Name="PART_EditableTextBox" Background="Transparent"
VerticalAlignment="Center" Style="{x:Null}" IsReadOnly="False" Focusable="True" xml:space="preserve" />
<Popup Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" IsOpen="{TemplateBinding IsDropDownOpen}" PopupAnimation="Fade">
<Grid MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}" x:Name="DropDown" SnapsToDevicePixels="True">
<Border BorderBrush="#FF646464" BorderThickness="1,1,1,1" x:Name="DropDownBorder" Background="{StaticResource WindowBackgroundBrush}"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" >
<ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" Margin="5,0,0,0" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEditable" Value="True">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
<Setter Property="Visibility" TargetName="PART_EditableTextBox" Value="Visible"/>
<Setter Property="Visibility" TargetName="ContentSite" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
和WPF窗口如下图:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox Name="combo1" ItemsSource="{Binding}" Style="{StaticResource ComboBoxStyle}"></ComboBox>
</Grid>
C#代码是:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataTable table = new DataTable("table1");
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Patient", typeof(string));
table.Rows.Add(25, "David");
table.Rows.Add(50, "Sam");
table.Rows.Add(10, "Christoff");
table.Rows.Add(21, "Janet");
table.Rows.Add(100, "Melanie");
combo1.DataContext = table;
combo1.DisplayMemberPath = "Patient";
combo1.SelectedValuePath = "Dosage";
}
当我将属性设置为 IsEditable=True 时,组合框可以正常工作,但当该属性为 False 时,它会将所选项目显示为 System.Data.DataRowView。风格有问题吗?