2012-05-28 29 views
1

我有简单的组合框模板,如下图所示:组合框选择节目System.Data.DataRowView

<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。风格有什么问题吗?

+1

试图缩短代码,我把它放在这里,这样很容易阅读。不要只写整个代码 –

+0

嗨,因为你有一个绑定到ComboBox的System.Data.DataRowView集合,因此选定的项目将是类型'System.Data.DataRowView' – ethicallogics

+0

我知道风格存在一些问题,而我不好。所以我已经把这个组合框使用的所有样式。没有风格的实际代码已经很短了。但是如果您在阅读时遇到问题,我很抱歉。 –

回答

1

这是风格问题,正如我所说,我错过了在ContentPresenter中添加ContentTemplateSelector =“{TemplateBinding ItemTemplateSelector}”。当我添加它时已解决。