将ObservableCollection绑定到Lines

时间:2013-03-15 06:53:17

标签: wpf data-binding user-controls

我希望在UserControl中有一个图形部分,它将根据集合显示Lines(并在运行时更新coords,并根据集合在运行时添加/删除行)。

假设我有一个名为Class1的类:

public class Class1 : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        private int _x1 = 0;
        public int X1
        {
            get { return _x1; }
            set
            {
                _x1 = value;
                OnPropertyChanged(new PropertyChangedEventArgs("X1"));
            }
        }
    }

我的UserControl中有一个ObservableCollection:

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        private ObservableCollection<Class1> _classes = new ObservableCollection<Class1>();
        public ObservableCollection<Class1> Classes
        {
            get { return _classes; }
        }

如果我知道行的数量,我可以在我的UserControl中创建行数,如下所示:

XAML:

<Grid DataContext="{Binding ElementName=LayoutRoot}">
<Line X1="{Binding Items[0].X1}" X2="100" Y1="50" Y2="100" Stroke="Red" StrokeThickness="4"/>
<Line X1="{Binding Items[1].X1}" ... />
...
</Grid>

我该怎么办?

感谢您的任何努力

2 个答案:

答案 0 :(得分:2)

您可以使用ItemsControlDataTemplate对象创建Class1,并将ObservableCollection绑定到ItemsControl

如果你想在整个地方画线,最好使用Canvas作为ItemsPanelTemplate

这是一个简单的例子:

的Xaml:

<Grid DataContext="{Binding ElementName=UI}">
    <ItemsControl ItemsSource="{Binding Classes}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type local:Class1}">
                <Line Stroke="Black" StrokeThickness="2" Fill="Black" X1="{Binding X1}" X2="{Binding X2}" Y1="{Binding Y1}" Y2="{Binding Y2}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

代码:

public partial class MainWindow : Window
{

    public MainWindow() 
    {
        InitializeComponent();
        Random ran = new Random();
        for (int i = 0; i < 10; i++)
        {
            Classes.Add(new Class1 { X1 = ran.Next(0,100), Y1 = ran.Next(0,100), X2 = ran.Next(0,100), Y2 = ran.Next(0,100) }); 
        }
    }

    private ObservableCollection<Class1> _classes = new ObservableCollection<Class1>();
    public ObservableCollection<Class1> Classes
    {
        get { return _classes; }
    }

}

结果:

enter image description here

答案 1 :(得分:1)

您可以使用ItemsControl作为容器来显示您的行。

<ItemsControl DataContext="{Binding ElementName=LayoutRoot}" 
              ItemsSource={Binding}>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Line X1="{Binding Items[0].X1}" X2="100" Y1="50" Y2="100" Stroke="Red" StrokeThickness="4"/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>