关于在canvas中的setbinding

时间:2012-03-24 06:47:01

标签: wpf

<StackPanel Height="650" Width="650" Background="Green" HorizontalAlignment="Center" VerticalAlignment="Center"> <ItemsControl Name="Display_Test" Margin="10"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Background="Black" Width="600" Height="600"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <ContentPresenter Content="{Binding}" Margin="15" Height="500" Width="500"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </StackPanel>

// c#code start ========================================== ==========================  public partial class MainWindow:Window,INotifyPropertyChanged     {         public MainWindow()         {             this.InitializeComponent();

        Canvas canvasSample = new Canvas();
        canvasSample.Width = 100;
        canvasSample.Height = 100;
        canvasSample.Background = new SolidColorBrush(Colors.LightBlue);
        CanvasList.Add(canvasSample);

        Canvas canvasSample2 = new Canvas();
        canvasSample2.Width = 100;
        canvasSample2.Height = 100;
        canvasSample2.Background = new SolidColorBrush(Colors.LightGreen);

        CanvasList.Add(canvasSample2);

        Rectangle rectangleSample = new Rectangle();
        rectangleSample.Width = 30;
        rectangleSample.Height = 30;
        RectangleList.Add(rectangleSample);

        Rectangle rectangleSample2 = new Rectangle();
        rectangleSample2.Width = 30;
        rectangleSample2.Height = 30;
        RectangleList.Add(rectangleSample2);

        Display_Test.ItemsSource = CanvasList;

        this.MouseDoubleClick += new MouseButtonEventHandler(MainWindow_MouseDoubleClick);
    }

    void MainWindow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {

        for (int nCanvasNum = 0; nCanvasNum < CanvasList.Count; nCanvasNum++)
        {
            Binding conBinding = new Binding()
            {
                Source = RectangleList
            };
            CanvasList[nCanvasNum].DataContext = RectangleList;
            CanvasList[nCanvasNum].SetBinding(ItemsControl.ItemsSourceProperty, conBinding);
        }
    }

    ObservableCollection<Canvas> _canvasList = new ObservableCollection<Canvas>();
    public ObservableCollection<Canvas> CanvasList
    {
        get { return _canvasList; }
        set
        {
            _canvasList = value;
            OnPropertyChanged(new PropertyChangedEventArgs("CanvasList"));
        }
    }

    ObservableCollection<Rectangle> _rectangleList = new ObservableCollection<Rectangle>();
    public ObservableCollection<Rectangle> RectangleList
    {
        get { return _rectangleList; }
        set
        {
            _rectangleList = value;
            OnPropertyChanged(new PropertyChangedEventArgs("RegtangleList")); 
        }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
    #endregion
}

// c#code end ========================================== ==========================

当我点击mousebutton double我想在CanvasList中创建RectangleList ... 但是我害怕我无法表明......我认为这是因为装订的问题...... 我会等你的回答......

1 个答案:

答案 0 :(得分:0)

CanvasList[nCanvasNum].SetBinding(ItemsControl.ItemsSourceProperty, conBinding);将无效,因为Canvas不是从ItemsControl派生的,也没有ItemSource属性。它有子属性,但它不是依赖属性,所以你不能绑定它,但你可以手动添加子。您可以尝试使用ItemsControl而不是Canvas,并将Canvas设置为ItemsControl的面板,就像您对Show canvase一样