2010-08-21 49 views
0

我有這樣的代碼:一些WPF Font_Combobox問題

<ComboBox Width="100" ItemsSource="{Binding FontList}" x:Name="fontComboFast"> 
         <ComboBox.ItemsPanel> 
          <ItemsPanelTemplate> 
           <VirtualizingStackPanel /> 
          </ItemsPanelTemplate> 
         </ComboBox.ItemsPanel> 
         <ComboBox.ItemTemplate> 
          <DataTemplate> 
           <TextBlock Text="{Binding}" FontFamily="{Binding }" FontSize="12" /> 
          </DataTemplate> 
         </ComboBox.ItemTemplate> 
        </ComboBox> 

存在這樣的組合框3個陷阱。

  1. 的項目/字體有不同的高度
  2. 當我向上/向下滾動寬度增大/減小取決於最長的可見項在ScrollViewer中長度scrollviewer`s。我怎樣才能設置一個固定的寬度?
  3. 又名的TextBlocks字體未垂直居中

我怎樣才能改變這些三件事情?

UPDATE

<ComboBox AlternationCount="2" Width="200" ItemContainerStyle="{StaticResource alternateColor}" ItemsSource="{Binding Source={x:Static Member=Fonts.SystemFontFamilies}}" x:Name="fontComboFast"> 

<Style x:Key="alternateColor" TargetType="{x:Type ComboBoxItem}"> 
      <Style.Setters> 
       <Setter Property="Height" Value="30" /> 
       <Setter Property="VerticalContentAlignment" Value="Center" /> 
       <Setter Property="FontSize" Value="16" /> 
      </Style.Setters> 
      <Style.Triggers>     
       <Trigger Property="ItemsControl.AlternationIndex" Value="0"> 
        <Setter Property="Background" Value="LightGray"/> 
       </Trigger> 
       <Trigger Property="ItemsControl.AlternationIndex" Value="1"> 
        <Setter Property="Background" Value="AliceBlue"/> 
       </Trigger>     
      </Style.Triggers> 
     </Style> 

哼的3個回答2是正確的,他們是最簡單的的人是這樣的,現在的解決方案? :O 商店裏有一些很酷的組合框提示?然後我會標記爲解決方案,否則你會得到一個點;-)

btw。祝賀你的新wpf工作在你的博客上閱讀,我羨慕你!

回答

1
  1. 兩個選擇 - 一)不那麼漂亮:設置一個固定的高度TextBlock中的或b)把項目這樣的網格內:

    <ComboBox ... Grid.IsSharedSizeScope="True"> 
        <ComboBox.ItemTemplate> 
         <DataTemplate> 
          <Grid> 
           <Grid.RowDefinistions> 
            <RowDefinition Height="Auto" SharedSizeGroup="Row"/> 
           <Grid.RowDefinistions> 
           <TextBlock .../> 
          <Grid> 
         <DataTemplate> 
        <ComboBox.ItemTemplate> 
    
  2. 再次 - 雙選項:a)在DataTemplate中設置固定寬度的TextBlock。 b)如果用StackPanel替換VirtualizingStackPanel,並且對於上面的ColumnDefinition執行相同的操作(如果列表中有很多內容,將會導致性能問題,因爲它會在加載時創建所有視覺元素。

  3. Put VerticalAlignment 。= 「中心」 在DataTemlate裏面的TextBlock

希望這有助於

編輯:

謝謝:)。我給你一些提示:

當使用VirtualizingStackPanel,在幾乎所有情況下,你應該設置VirtualizationMode =「回收」 - 同樣適用於順便把其他ItemsControls:

<ListBox VirtualizingStackPanel.VirtualiationMode="Recycling"/> 
<VirtualizingStackPanel VirtualizationMode="Recycling"/> 

這將當用戶滾動列表時回收DataTemplate的。特別是在大型數據集或複雜的DataTemplates中,這將帶來相當順暢的體驗。 IsEditable =「True」破壞了這個好處(這是一個已知的錯誤)。

通常當你只想使用一個屬性作爲DataTemplate時,你可以使用DisplayMemberPath - 這會給你鍵盤快捷鍵(鍵入'T'將滾動到以T等開頭的第一項)。如果你使用DataTemplates - 你可以通過使用TextSearch.TextPath來達到同樣的效果。只要記住要將組合框中的項目排序爲您在TextPath中使用的相同屬性 - 否則,用戶會遇到「顛簸」的體驗,因爲它看起來會隨機跳到列表中。

如果你想在列表中的色彩呈現每第二個項目 - 你可以做到這一點,如下所示:

<UserControl.Resources> 
    <Style TargetType="{x:Type ComboBoxItem}"> 
     <Style.Triggers> 
      <Trigger Property="ItemsControl.AlternationIndex" Value="1"> 
       <Setter Property="Background" Value="LightGray"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 
<ComboBox AlternationCount="2"> 

其實,我真的不使用組合框那麼多 - 主要是,我用它來枚舉值和非常小的數據集。 ComboBox的問題在於它支持非常糟糕的分頁 - 對於大型數據集,我通常使用WPF Toolkit中的AutoCompleteBox或上面帶有TextBox的ListBox進行過濾。

希望,你有在那裏有一些提示:-)

+0

1b.worked不 2a.worked 3..worked – Elisabeth 2010-08-21 22:11:30

+0

我在這裏張貼圖片,所以你知道我的意思,你看到的不端行爲: http://666kb.com/i/bm0et3s0n9atkmzol.png – Elisabeth 2010-08-22 08:35:47

+0

好吧,我得到它看到我的更新init後。 – Elisabeth 2010-08-22 08:47:08