2012-05-28 61 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}」。當我添加它時已解決。