2011-09-20 16 views
2

可以說我有一個大的bitset(類無關緊要,可以布爾[100]),我得到10x10矩形的黑色和白色,我想綁定到每個單獨的位在我的bitset。控制每位綁定到大bitset

我還希望當單個位更改(有一些可能導致此行爲的解決方案)時不強制完全重新繪製,而只是對該特定矩形進行一次重繪。

關於我的實際實現的任何其他細節並不重要,我可以使用任何適合最好的類來存儲這些位(ObservableCollection,其他的,你的名字)。

我更喜歡最優雅的解決方案,我也不想放大視圖模型100個屬性..和巨大的案例方法。

回答

1

這是一個有趣的測試解決方案的問題。 :)

這是我想出來的。點擊播放按鈕隨機觀看顏色。

查看:

<UserControl 
    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" 
    xmlns:vm="clr-namespace:Samples.ViewModels" 
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="Samples.BitsetView" 
    mc:Ignorable="d" 
    d:DesignHeight="400" d:DesignWidth="500"> 

    <UserControl.Resources> 
     <vm:BitsetViewModel x:Key="vm" /> 
     <ItemsPanelTemplate x:Key="ItemsPanelTemplate"> 
      <toolkit:WrapPanel /> 
     </ItemsPanelTemplate> 
     <DataTemplate x:Key="ItemTemplate"> 
      <Rectangle Width="10" Height="10" Fill="{Binding Brush}"/> 
     </DataTemplate> 
    </UserControl.Resources> 

    <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource vm}" Width="500" Height="400"> 
     <ItemsControl Margin="0" ItemsSource="{Binding Bitset}" ItemsPanel="{StaticResource ItemsPanelTemplate}" ItemTemplate="{StaticResource ItemTemplate}"/> 
     <Button Content="{Binding Action}" Height="23" Width="40" HorizontalAlignment="Center" VerticalAlignment="Bottom"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="Click"> 
        <ei:CallMethodAction TargetObject="{Binding}" MethodName="Play"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Button> 
    </Grid> 
</UserControl> 

和視圖模型:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Windows.Media; 
using System.Windows.Threading; 

namespace Samples.ViewModels 
{ 
    public class BitsetViewModel 
    { 
     private List<BitsetItem> _bitset = new List<BitsetItem>(); 
     private Random _rand = new Random(); 
     private DispatcherTimer _timer = new DispatcherTimer(); 

     public BitsetViewModel() 
     { 
      _timer.Interval = TimeSpan.FromMilliseconds(1); 
      _timer.Tick += new EventHandler(_timer_Tick); 

      for (int i = 0; i < 2000; i++) 
      { 
       var color = _rand.Next(0, 5); 
       _bitset.Add(new BitsetItem() { Color = color == 1 ? Colors.Black : Colors.White }); 
      } 
     } 

     void _timer_Tick(object sender, EventArgs e) 
     { 
      var bit = _rand.Next(0, 1999); 
      _bitset[bit].Color = _bitset[bit].Color == Colors.White ? Colors.Black : Colors.White; 
     } 

     public IEnumerable<BitsetItem> Bitset 
     { 
      get {return _bitset;} 
     } 

     public string Action 
     { 
      get { return _timer.IsEnabled ? "Stop" : "Play"; } 
     } 

     public void Play() 
     { 
      if (_timer.IsEnabled) 
       _timer.Stop(); 
      else 
       _timer.Start(); 
     } 
    } 

    public class BitsetItem : INotifyPropertyChanged 
    { 
     private Color _color = Colors.White; 
     private SolidColorBrush _brush = new SolidColorBrush(); 

     public Color Color 
     { 
      get { return _color; } 
      set 
      { 
       _color = value; 
       _brush = new SolidColorBrush(Color); 
       RaisePropertyChanged("Color"); 
       RaisePropertyChanged("Brush"); 
      } 
     } 

     public SolidColorBrush Brush 
     { 
      get { return _brush; } 
      set 
      { 
       _brush = value; 
       RaisePropertyChanged("Brush"); 
      } 
     } 

     private void RaisePropertyChanged(string name) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(name)); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 
} 
+0

你可以添加對這種做法表現某種思想。如果沒有其他人對此感興趣,那麼我會把它標記爲答案。 –

+0

當我在機器上運行它時,它非常高效。你有什麼擔憂? – Bryant

+0

我主要關心你的循環。循環中改變單個位。我想知道,如果您循環使用2000個bitsets,它將會是多麼的高性能。無論如何,我可以自己做到這一點,但更普遍的是有沒有辦法讓它的表現更快?這種方法有什麼問題。由於沒有其他選擇,我對這一解決方案的特點感興趣。 –