2011-11-17 34 views
3

我希望在運行時更改ListBox的ItemsPanelTemplate。在運行時更改ListBox的ItemsPanelTemplate

我有以下XAML,它允許我更改ItemsPanelTemplate;但是會破壞ScrollViewer的不良副作用。

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:ie="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" 

... 

<UserControl.Resources> 
    <ItemsPanelTemplate x:Key="StackPanelTemplate"> 
     <VirtualizingStackPanel/> 
    </ItemsPanelTemplate> 

    <ItemsPanelTemplate x:Key="WrapPanelTemplate"> 
     <telerik:RadWrapPanel/> 
    </ItemsPanelTemplate> 
</UserControl.Resources> 

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <StackPanel> 
     <Button Content="StackPanel"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <ie:ChangePropertyAction TargetName="TargetListBox" PropertyName="ItemsPanel" Value="{StaticResource StackPanelTemplate}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Button> 
     <Button Content="WrapPanel"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <ie:ChangePropertyAction TargetName="TargetListBox" PropertyName="ItemsPanel" Value="{StaticResource WrapPanelTemplate}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Button> 
    </StackPanel> 

    <ListBox x:Name="TargetListBox" Grid.Column="1" ItemsSource="{Binding SomeCollection}"/> 
</Grid> 

當你改變ItemsPanelTemplate這種方式。在您更改ScrollViewer之前,ScrollViewer似乎處於任何狀態,並且使用滾動條不會影響ListBox上的任何更改。

任何人都可以提供有關此問題的任何見解或提供解決方法?

謝謝。

*編輯*

所以,我到它正在與虛擬化縮小的問題。如果您只是將常規StackPanel更改爲VirtualizingStackPanel,則ScrollViewer不會中斷。這對我來說並不是一個真正的解決方案,但是這個ListBox可以容納數百個搜索結果。

回答

1

我認爲最簡單的解決方法是替換整個ListBox而不是面板模板。

+0

感謝您的回覆。是的,我曾想到這一點。當你說「替換」時,我假設你的意思是有兩個ListBox並綁定它們的可見性?如果是這樣;我希望有一個更優雅的解決方案。 – Dan

+0

我實際上正在考慮從邏輯樹中移除ListBox並在其位置插入另一個ListBox,或者您可以使用DataTemplateSelector(如Prism中的那個),它將顯示特定類型的ListBox,具體取決於您的習慣。 –

0

好吧,我正面臨着同樣的問題,我想創建一個有產品的列表框,並讓用戶可以自由地將佈局從WrapPanel更改爲列表框,並將列表框從列表框更改爲WrapPanel。所以要做到這一點,你應該使用樣式。 (我建議你使用ListView而不是ListBox,因爲ListBox中的滾動問題。無論如何都可以)。 先加你2款內App.xaml中

WrapPanelTemplateLV

<Style x:Key="WrapPanelTemplateLV" TargetType="ListView"> 
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> 
      <Setter Property="ItemsPanel"> 
       <Setter.Value> 
        <ItemsPanelTemplate> 
         <WrapPanel /> 
        </ItemsPanelTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

和StackPanelTemplateLV

<Style x:Key="StackPanelLV" TargetType="ListBox"> 
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> 
      <Setter Property="ItemsPanel"> 
       <Setter.Value> 
        <ItemsPanelTemplate> 
         <StackPanel /> 
        </ItemsPanelTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
現在你的按鈕內

只是這樣做

現在你明白了。使一些邏輯在兩種樣式之間切換。 我建議你去更多的這個問題。 Changing the styles at runtime in WPF