我有一个UniformGrid。在里面,我想放一个包含两个孩子的Grid,以及一个Image和一个Canvas。我已经有了一个List<Grid>成员,它包含一个具有该定义的Grid。
我正在将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>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正确地更新了。
我遗漏了什么?
发布于 2015-06-18 21:06:00
也许问题是您的项根本没有显示--不是图像没有更新(因为我看到您正在直接更新图像元素的源属性).对于列表,PropertyChanged事件只在更改List<>时才会触发,而不是在向其添加某些内容时触发,而与添加/删除/替换/清除项时触发事件的ObservableCollection<>不同。
尝试将您的List<>替换为ObservableCollection<> (在System.Collections.ObjectModel中找到)
除此之外,Grid和Image可以在ItemsControl.ItemTemplate中的DataTemplate中定义,Source绑定到流ViewModel的属性(这是您应该拥有的集合) --但与问题无关。
https://stackoverflow.com/questions/30924845
复制相似问题