2010-10-22 130 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