C# 未在WPF数据模板中绘制线

C# 未在WPF数据模板中绘制线,c#,wpf,xaml,wpf-controls,datatemplate,C#,Wpf,Xaml,Wpf Controls,Datatemplate,我正在尝试收集正确的WPF数据模板,以获得一个Listview,该Listview对于每个项目都有一个Border+画布(请参见),然后在该画布上为最终节点集合中的每个项目绘制一条线。它几乎可以工作,但目前我在画布上没有画线:在调试输出窗口中,我看到: System.Windows.Data错误:40:BindingExpression路径错误:“在对象”“CalloutGroup'(HashCode=35061213)”上找不到CalloutGroup属性。BindingExpression:

我正在尝试收集正确的WPF数据模板,以获得一个Listview,该Listview对于每个项目都有一个Border+画布(请参见),然后在该画布上为最终节点集合中的每个项目绘制一条线。它几乎可以工作,但目前我在画布上没有画线:在调试输出窗口中,我看到:

System.Windows.Data错误:40:BindingExpression路径错误:“在对象”“CalloutGroup'(HashCode=35061213)”上找不到CalloutGroup属性。BindingExpression:Path=callout组;DataItem='CalloutGroup'(HashCode=35061213);目标元素是“ItemsControl”(名称=“”);目标属性为“ItemsSource”(类型为“IEnumerable”)

这是MainWindow.Xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CalloutControl" x:Class="CalloutControl.MainWindow"
        Title="MainWindow" Height="430.597" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="LineTemplate">
            <Line X1 ="{Binding X}" X2="{Binding X}" Stroke="Wheat" 
                      StrokeThickness="10" Y1="{Binding Y}" Y2="{Binding Y}"/>
        </DataTemplate>


        <DataTemplate x:Key="GroupTemplate">
        <Border Width="200" Height ="{Binding DistributionHeight}" BorderThickness="5" Background="Brown" BorderBrush="Red">
                <ItemsControl ItemsSource="{Binding Path=CalloutGroup}" 
                          ItemTemplate="{StaticResource LineTemplate}">
                    <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Background="Beige"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Border>
        <ListView ItemsSource="{Binding Groups}"
                      ItemTemplate="{StaticResource GroupTemplate}">

        </ListView>
        </Border>
    </Grid>
</Window>

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 CalloutControl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new MainWindowViewModel();
            DataContext = vm;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间调用控件
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
var vm=新的MainWindowViewModel();
DataContext=vm;
}
}
}
然后单击ViewModel->MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace CalloutControl
{
    public class CalloutItem            : INotifyPropertyChanged
    {
        public CalloutItem()
        {

        }
        public CalloutItem (int length,
                   int itemNum,
                     double x,
                    double y ){
                        Length = length;
                        ItemNum = itemNum;
                        X = x;
                        Y =  y;
        }
        private int length;

        public int Length
        {
            get { return length; }
            set
            {
                length = value;
                OnPropertyChanged("Length");
            }
        }
        private int itemNum;

        public int ItemNum
        {
            get { return itemNum; }
            set { itemNum = value;
            OnPropertyChanged("ItemNum");
            }
        }
        //public int      ItemNum;
        private double x;

        public double X
        {
            get { return x; }
            set { x = value;
                  OnPropertyChanged("X");
            }
        }
        private double y;

        public double Y
        {
            get { return y; }
            set { y = value;
            OnPropertyChanged("Y");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    public class CalloutGroup           : INotifyPropertyChanged
    {
        ObservableCollection<CalloutItem> group = new ObservableCollection<CalloutItem>();
        public CalloutGroup(int nItems)
        {
            NumItems = nItems;

        }
        public ObservableCollection<CalloutItem> Group
        {
            get { return group; }
            set
            {
                group = value;
                OnPropertyChanged("Group");
            }
        }
        private double distributionHeight = 400;

        public double DistributionHeight
        {
            get { return distributionHeight; }
            set
            {
                distributionHeight = value;
                Redistribute();
                OnPropertyChanged("DistributionHeight");
            }
        }


        private void Redistribute()
        {
            double step = distributionHeight / numItems;
            double currY = step / 2 ;
            var redistArr = group.ToArray();

            for (int i = 0; i < group.Count(); i++)
            {
                redistArr[i].Y = currY;
                currY += step;
            }
            OnPropertyChanged("Group");
        }
        private int numItems = 10;

        public int NumItems
        {
            get { return numItems; }
            set
            {
                numItems = value;
                group.Clear();
                for (int i = 0; i < numItems; i++)
                {
                    group.Add(new CalloutItem());
                }
                Redistribute();
                OnPropertyChanged("NumItems");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    public class MainWindowViewModel    : INotifyPropertyChanged
    {

        ObservableCollection<CalloutGroup> groups = new ObservableCollection<CalloutGroup>();

        public ObservableCollection<CalloutGroup> Groups
        {
            get { return groups; }
            set { groups = value;
                  OnPropertyChanged("Groups");
            }
        }
        public MainWindowViewModel ()
        {
            groups.Add(new CalloutGroup(3));
            groups.Add(new CalloutGroup(3));

        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Collections.ObjectModel;
命名空间调用控件
{
公共类标注项:INotifyPropertyChanged
{
公共物品(
{
}
公共项(整数长度,
int itemNum,
双x,
双y){
长度=长度;
ItemNum=ItemNum;
X=X;
Y=Y;
}
私有整数长度;
公共整数长度
{
获取{返回长度;}
设置
{
长度=值;
不动产变更(“长度”);
}
}
私有int itemNum;
公共int ItemNum
{
获取{return itemNum;}
设置{itemNum=value;
OnPropertyChanged(“ItemNum”);
}
}
//公共int ItemNum;
私人双x;
公共双X
{
获取{return x;}
设置{x=值;
不动产变更(“X”);
}
}
私人双y;
公共双Y
{
获取{返回y;}
设置{y=值;
不动产变更(“Y”);
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
}
公共类Callout组:INotifyPropertyChanged
{
ObservableCollection组=新的ObservableCollection();
公共呼叫组(int nItems)
{
NumItems=nItems;
}
公共可观测收集组
{
获取{返回组;}
设置
{
组=值;
不动产变更(“集团”);
}
}
私人双分配高度=400;
公共双分配高度
{
获取{return distributionHeight;}
设置
{
分布高度=数值;
重新分配();
OnProperty变更(“分配高度”);
}
}
私有空间重新分配()
{
双台阶=分布高度/单位;
双咖喱=step/2;
var redistArr=group.ToArray();
对于(int i=0;i<ItemsControl ItemsSource="{Binding Group}"
<ItemsControl ItemsSource="{Binding Path=CalloutGroup}"`
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 CalloutControl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new MainWindowViewModel();

            vm.Groups.Add(new CalloutControl.CalloutGroup(2));
            vm.Groups.Add(new CalloutControl.CalloutGroup(2));

            vm.Groups[0].Group.Add(new CalloutControl.CalloutItem(10, 1, 0, 100, 200, 100));
            vm.Groups[0].Group.Add(new CalloutControl.CalloutItem(10, 2, 100, 0, 100, 200));

            vm.Groups[1].Group.Add(new CalloutControl.CalloutItem(10, 1, 100, 0, 200, 100));
            vm.Groups[1].Group.Add(new CalloutControl.CalloutItem(10, 2, 100, 200, 200, 100));

            DataContext = vm;
        }
    }
}

namespace CalloutControl
{
    public class CalloutItem : INotifyPropertyChanged
    {
        public CalloutItem()
        {

        }
        public CalloutItem(int length,
                   int itemNum,
                     double _x1,
                    double _y1,
            double _x2,
            double _y2)
        {
            Length = length;
            ItemNum = itemNum;
            X1 = _x1;
            Y1 = _y1;
            X2 = _x2;
            Y2 = _y2;
        }
        private int length;

        public int Length
        {
            get { return length; }
            set
            {
                length = value;
                OnPropertyChanged("Length");
            }
        }
        private int itemNum;

        public int ItemNum
        {
            get { return itemNum; }
            set
            {
                itemNum = value;
                OnPropertyChanged("ItemNum");
            }
        }
        //public int      ItemNum;
        private double x1;
        public double X1
        {
            get { return x1; }
            set
            {
                x1 = value;
                OnPropertyChanged("X1");
            }
        }
        private double y1;
        public double Y1
        {
            get { return y1; }
            set
            {
                y1 = value;
                OnPropertyChanged("Y1");
            }
        }

        private double x2;
        public double X2
        {
            get { return x2; }
            set
            {
                x2 = value;
                OnPropertyChanged("X2");
            }
        }
        private double y2;
        public double Y2
        {
            get { return y2; }
            set
            {
                y2 = value;
                OnPropertyChanged("Y2");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    public class CalloutGroup : INotifyPropertyChanged
    {
        ObservableCollection<CalloutItem> group = new ObservableCollection<CalloutItem>();
        public CalloutGroup(int nItems)
        {
            NumItems = nItems;

        }
        public ObservableCollection<CalloutItem> Group
        {
            get { return group; }
            set
            {
                group = value;
                OnPropertyChanged("Group");
            }
        }
        private double distributionHeight = 200;

        public double DistributionHeight
        {
            get { return distributionHeight; }
            set
            {
                distributionHeight = value;
                Redistribute();
                OnPropertyChanged("DistributionHeight");
            }
        }


        private void Redistribute()
        {
            double step = distributionHeight / numItems;
            double currY = step / 2;
            var redistArr = group.ToArray();

            for (int i = 0; i < group.Count(); i++)
            {
                redistArr[i].Y1 = currY;
                currY += step;
            }
            OnPropertyChanged("Group");
        }
        private int numItems = 10;

        public int NumItems
        {
            get { return numItems; }
            set
            {
                numItems = value;
                group.Clear();
                for (int i = 0; i < numItems; i++)
                {
                    //group.Add(new CalloutItem());
                }
                Redistribute();
                OnPropertyChanged("NumItems");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    public class MainWindowViewModel : INotifyPropertyChanged
    {

        ObservableCollection<CalloutGroup> groups = new ObservableCollection<CalloutGroup>();

        public ObservableCollection<CalloutGroup> Groups
        {
            get { return groups; }
            set
            {
                groups = value;
                OnPropertyChanged("Groups");
            }
        }
        public MainWindowViewModel()
        {

        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
<Window.Resources>
    <DataTemplate x:Key="LineTemplate">
        <Line X1="{Binding X1}" X2="{Binding X2}"
              Y1="{Binding Y1}" Y2="{Binding Y2}"
              Stroke="Blue" 
                  StrokeThickness="10" />
    </DataTemplate>


    <DataTemplate x:Key="GroupTemplate">
        <Border BorderThickness="5" Background="Brown" BorderBrush="Red">
            <ItemsControl ItemsSource="{Binding Group}" 
                      ItemTemplate="{StaticResource LineTemplate}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                        <Canvas Background="Transparent" Width="200" Height="{Binding DistributionHeight}"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
        </Border>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Border>
        <ListView ItemsSource="{Binding Groups}" ItemTemplate="{StaticResource GroupTemplate}">

        </ListView>
    </Border>
</Grid>