2010-10-22 68 views
2

請看下面的代碼, 我點擊該按鈕後,列表框渲染幾次。 如何防止Listbox閃爍? 是否可以告訴控制停止更新/渲染?的RenderTransform閃爍

<UserControl x:Class="SilverlightApplication52.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     d:DesignHeight="300" 
     d:DesignWidth="400"> 

<Grid x:Name="LayoutRoot" 
     Background="Gray" 
     HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch"> 
    <ListBox x:Name="listbox" 
      Background="White" 
      Margin="100"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas /> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 

      </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Rectangle Width="{Binding Width}" 
          Height="{Binding Height}" 
          Fill="{Binding Background}" 
          RenderTransformOrigin="0.5,0.5"> 
        <Rectangle.RenderTransform> 
         <TransformGroup> 
          <ScaleTransform ScaleX="{Binding Scale}" 
              ScaleY="{Binding Scale}" /> 
          <RotateTransform Angle="{Binding Angle}" /> 
          <TranslateTransform X="{Binding Left}" 
               Y="{Binding Top}" /> 
         </TransformGroup> 
        </Rectangle.RenderTransform> 
       </Rectangle> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <Button Content="test" 
      Width="50" 
      Height="50" 
      Click="Button_Click" /> 
</Grid> 

public partial class MainPage : UserControl 
{ 
    public class ItemInfo 
    { 
     public double Left { get; set; } 
     public double Top { get; set; } 
     public double Width { get; set; } 
     public double Height { get; set; } 
     public double Angle { get; set; } 
     public double Scale { get; set; } 
     public Brush Background { get; set; } 
    } 

    ObservableCollection<ItemInfo> _items = new ObservableCollection<ItemInfo>(); 
    public MainPage() 
    { 
     InitializeComponent(); 
     listbox.ItemsSource = _items; 

    } 

    Random random = new Random(); 
    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     _items.Clear(); 
     for (int i = 0; i < 2000; i++) 
     { 
      byte r = (byte)(random.NextDouble()*255); 
      byte g = (byte)(random.NextDouble()*255); 
      byte b = (byte)(random.NextDouble()*255); 
      _items.Add(
       new ItemInfo 
       { 
        Left = random.NextDouble() * 500, 
        Top = random.NextDouble() * 500, 
        Width = random.NextDouble() * 1000, 
        Height = random.NextDouble() * 1000, 
        Angle = random.NextDouble() * 359, 
        Scale = random.NextDouble() * 1, 
        Background = new SolidColorBrush(Color.FromArgb(255,r,g,b)), 
       } 
      ); 
     } 
    } 
} 
+0

好像是在閃爍通過的RenderTransform, 列表框造成危害會使一次的RenderTransform的結合與更新綁定值獲得正確的綁定值(使用綁定的備用值) 並再次。 但仍然不知道如何解決這個問題。 – zunyite 2010-10-26 12:12:54

+0

發現了類似的問題http://forums.silverlight.net/forums/p/203971/477213.aspx – zunyite 2010-10-26 17:46:33

回答

1

嘗試增加在該循環的單獨的ObservableCollection(即不被引用/綁定到列表框)。然後當循環完成時,將listbox ItemsSource分配給新的observablecollection。

+0

謝謝,我嘗試分配列表框的ItemsSource一個新的ObservableCollection。但它沒有幫助。 – zunyite 2010-10-23 00:41:46