简体   繁体   English

WPF Canvas不会渲染绑定到ItemsControl的Shapes

[英]WPF Canvas does not render Shapes that are bound to the ItemsControl

I create Polygons in code that get added to an generic ObservableList. 我在代码中创建多边形,并将其添加到通用的ObservableList中。 That is my ViewModel. 那是我的ViewModel。

class CanvasViewModel : ModelBase
{
    private WpfObservableRangeCollection<Shape> CanvasShapes { get; set; }
//...
}

The Shape are proper WPF shapes. Shape是适当的WPF形状。 No container or anything. 没有容器或任何东西。 Furthermore I set the Style of these shapes also programmatically, as an example: 此外,我还以编程方式设置这些形状的样式,例如:

public void ApplyStyle(Style style)
    {
        style.Setters.Add(new Setter(Shape.FillProperty, Brushes.DodgerBlue));
    }

As you can see, I am definitely setting a color to these shapes. 如您所见,我肯定会为这些形状设置颜色。 My issue is that in my program, when I start it, the canvas gets rendered (I can see it when I set the background of the canvas to any color) but none of the shapes I add to the ObservableRangeCollection (they are definitely in the collection.) 我的问题是,在我的程序中,当我启动它时,画布被渲染(当我将画布的背景设置为任何颜色时我可以看到它)但是我没有添加到ObservableRangeCollection的形状(它们肯定是在采集。)

Here's the Page code for the CanvasPage: 这是CanvasPage的页面代码:

<ItemsControl ItemsSource="{Binding CanvasShapes}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

I figure since these are proper WPF shapes that I don't need to add any DataTemplates to them or anything, however I think this is where I'm mistaken. 我认为因为这些是正确的WPF形状,我不需要向它们或任何东西添加任何DataTemplates,但我认为这是我错的地方。

And here's the CodeBehind of the Window that currently initializes these functions (I know this is bad practice, it's just to get the programm running for testing purposes): 这里是Window的CodeBehind当前初始化这些函数(我知道这是不好的做法,它只是为了测试目的而运行程序):

public MainWindow()
    {
        InitializeComponent();
        CanvasPage canvasPage = new CanvasPage()
        //boilerplate...
        var canvasViewModel = new CanvasViewModel(...);
        canvasPage.DataContext = canvasViewModel;
        this._NavigationFrame?.Navigate(canvasPage);
    }

Here's a MCV version of this issue: 这是此问题的MCV版本:

Viewmodel.cs Viewmodel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace MVCE
{
    class ViewModel
    {
        public ObservableCollection<Shape> CanvasShapes;

        public ViewModel()
        {
            CanvasShapes = new ObservableCollection<Shape>();
            var polygon = new Polygon();
            var pointCollection = new PointCollection
            {
                new Point(100, 100),
                new Point(100, -100),
                new Point(-100, -100),
                new Point(-100, 100)
            };
            polygon.Points = pointCollection;
            Style style = new Style();
            style.Setters.Add( new Setter(Shape.FillProperty, Brushes.Aquamarine));

            polygon.Style = style;
            CanvasShapes.Add(polygon);
        }
    }
}

MainWindow.cs MainWindow.cs

<Window x:Class="MVCE.MainWindow"
        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:local="clr-namespace:MVCE"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <DockPanel>
        <Frame x:Name="_NavigationFrame" NavigationUIVisibility="Hidden" />
    </DockPanel>

</Window>

MainWindow.xaml.cs MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MVCE
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ViewModel viewModel = new ViewModel();
            var page = new Page1();
            page.DataContext = viewModel;
            _NavigationFrame.Navigate(page);
        }
    }
}

CanvasPage.xaml (.cs file is default) CanvasPage.xaml(.cs文件是默认的)

<Page x:Class="MVCE.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:MVCE"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page1">

    <ItemsControl ItemsSource="{Binding CanvasShapes}" Background="Brown">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Page>

I figured it out. 我想到了。

The issue was that my ObservableCollection in my ViewModel was set to private. 问题是我的ViewModel中的ObservableCollection被设置为私有。 Due to this, the DataBinding seemed to have issues. 因此,DataBinding似乎存在问题。 I thought it would work due to reflection, but I was mistaken. 我认为它会因反射而起作用,但我错了。 Setting the collection to public fixed it right away. 将集合设置为公共立即修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM