我是一个有用的助手,可以为您翻译文本。
我还可以看到
我错过了什么?
我有一个UniformGrid
。里面,我想放置一个包含2个子元素的Grid
- 一个Image
和一个Canvas
。我已经有一个List<Grid>
成员,其中包含了这样定义的Grid
。
我正在将Image
的源更新为实际图像,期望在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>
ViewModel:
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
正确更新。我错过了什么?