2015-06-18 26 views
1

我有一個UniformGrid。在裏面,我想把一個Grid其中包含2個孩子 - 和ImageCanvas。我已經有一個List<Grid>成員,其中包含具有該定義的GridUniformGrid和ItemsSource

我將Image的來源從null更新爲實際圖像,期待圖像顯示在UniformGrid的內部,但沒有任何反應。

這裏是我的代碼:

的XAML:

<Border Grid.Row="0" > 
     <ItemsControl x:Name="StreamsItemsControl" ItemsSource="{Binding Streams}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <UniformGrid x:Name="StreamsGrid" ClipToBounds="True" Height="300" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
     </ItemsControl> 
    </Border> 

視圖模型:

private List<Grid> m_streams = new List<Grid>(); 
public List<Grid> Streams 
     { 
      get { return m_streams; } 
      set 
      { 
       m_streams = value; 
       OnPropertyChanged("Streams"); 
      } 
     } 

編輯:添加更多的代碼:

public struct StreamContainer 
    { 
     public string StreamName; 
     public Grid StreamGrid; 
     public Canvas StreamCanvas; 
     public Image StreamImage; 
    } 
    private readonly List<StreamContainer> m_streamsContainer = new List<StreamContainer>(); 

    public SequencesPlayerViewModel() 
    { 
     RegisterStream("Color"); 
     RegisterStream("Depth"); 
     RegisterStream("Ir"); 
     RegisterStream("Left"); 
    } 

    private void RegisterStream(string streamName) 
    { 
     StreamContainer streamContainer; 

     streamContainer.StreamName = streamName; 
     streamContainer.StreamCanvas = new Canvas { Name = streamName + "Canvas", Background = Brushes.Transparent, VerticalAlignment = VerticalAlignment.Top }; 
     streamContainer.StreamImage = new Image { Name = streamName + "Image", Stretch = Stretch.Uniform, VerticalAlignment = VerticalAlignment.Top }; 

     var widthBinding = new Binding { Path = new PropertyPath("ActualWidth") }; 
     var heightBinding = new Binding 
     { 
      Path = new PropertyPath("ActualHeight"), 
      Source = streamContainer.StreamImage 
     }; 


     streamContainer.StreamCanvas.SetBinding(FrameworkElement.HeightProperty, heightBinding); 
     streamContainer.StreamCanvas.SetBinding(FrameworkElement.WidthProperty, widthBinding); 

     streamContainer.StreamGrid = new Grid { Name = streamName + "Grid" }; 
     streamContainer.StreamGrid.Children.Add(streamContainer.StreamImage); 
     streamContainer.StreamGrid.Children.Add(streamContainer.StreamCanvas); 

     streamContainer.StreamGrid.Visibility = Visibility.Collapsed; 

     m_streamsContainer.Add(streamContainer); 
    } 

    private void AddStream(StreamContainer currentStream) 
    { 
     var listOfStreams = GetListOfStream(); 

     if (listOfStreams.Count > 0) 
     { 
      var streamToAdd = listOfStreams.Find(currStream => currStream.Name == currentStream.StreamName); 
      if (streamToAdd != null) 
       if (!String.IsNullOrEmpty(currentStream.StreamName)) 
       { 
        currentStream.StreamGrid.Visibility = Visibility.Visible; 
        (currentStream.StreamGrid.Children[0] as Image).Source = streamToAdd.ImageBitmap; 
       } 
     } 

     Streams.Add(currentStream.StreamGrid); 
    } 

    private void OnNewFrameReady(uint frameNumber) 
    { 
     try 
     { 
      var listOfStreams = GetListOfStream(); 

      foreach (var elem in Streams) 
      { 
       var currentCanvas = (elem.Children[1] as Canvas); 
       var canvasName = currentCanvas.Name.Split(new[] { "Canvas" }, StringSplitOptions.RemoveEmptyEntries).First(); 

       var currentStream = listOfStreams.Find(currStream => currStream.Name == canvasName); 
       if (!String.IsNullOrEmpty(currentStream.Name)) 
        (elem.Children[0] as Image).Source = currentStream.ImageBitmap; 
       Panel p = currentCanvas; 
       elem.UpdateLayout(); 

       elem.Width = (elem.Children[0] as Image).ActualWidth; 
       elem.Height = (elem.Children[0] as Image).ActualHeight; 
       elem.UpdateLayout(); 
      } 
     } 
     catch (Exception ex) 
     { 
     } 
    } 

ViewModel連接正確,其他控件的Bindings正常工作。

我還可以看到,Streams成員已正確更新,這意味着Image已正確更新爲新的Source

我錯過了什麼?

+0

您是否將項目添加到列表中並期望它們顯示?您是否在網格列表已經加載後需要更改圖像源(需要向我們顯示用於綁定的代碼)? – bdimag

+0

好吧,我添加了更多的代碼。基本上我將所有'streams'註冊到'List'。然後,在某個時候,我打電話給'OnNewFrameReady'函數,它應該更新'UniformGrid'。至於你的問題:是的,我將項目添加到列表中,期待它們出現,並且對於第二個問題也是。 – Idanis

回答

1

也許問題是你的項目根本沒有顯示 - 不是圖像沒有更新(因爲我看到你正在更新圖像元素的源屬性直接)...與一個列表中,PropertyChanged事件只會在List<>發生更改時觸發,而不是在添加內容時發生,不同於ObservableCollection<>在添加/刪除/替換/清除項目時觸發事件。

嘗試更換您的List<>ObservableCollection<>(在System.Collections.ObjectModel找到)

除此之外,該GridImage可以改爲在ItemsControl.ItemTemplateDataTemplateSource綁定到的屬性來定義ViewModel(這是你應該收集的) - 但與問題無關。

+0

工作!我用'ObservableCollection'替換了'List',它就像一個魅力一樣。非常感謝!!! – Idanis