gpt4 book ai didi

wpf - UniformGrid 和 ItemsSource

转载 作者:行者123 更新时间:2023-12-02 01:36:32 29 4
gpt4 key购买 nike

我有一个 UniformGrid .在里面,我想放一个Grid其中包含 2 个 child - 和 ImageCanvas .我已经有一个 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>

查看型号:
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 .

我错过了什么?

最佳答案

也许问题是您的项目根本没有显示 - 不是图像没有更新(因为我看到您正在直接更新图像元素的源属性)......使用列表,PropertyChanged 事件只会在 List<> 更改时触发,而不是在向其中添加内容时触发,这与 ObservableCollection<> 在添加/删除/替换/清除项目时触发事件不同。

尝试用 List<> 替换您的 ObservableCollection<> (在 System.Collections.ObjectModel 中找到)

除此之外,GridImage 可以改为在 DataTemplate 内的 ItemsControl.ItemTemplate 中定义,并且 Source 绑定(bind)到流 ViewModel 的属性(这是您应该拥有的集合) - 但与问题无关。

关于wpf - UniformGrid 和 ItemsSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30924845/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com