简体   繁体   English

UWP canvas动态内容

[英]UWP canvas dynamic content

I want to be able to dynamically add different (self-created) "widgets" to my canvas and position them using Canvas.Top and Canvas.Left . I want to be able to dynamically add different (self-created) "widgets" to my canvas and position them using Canvas.Top and Canvas.Left .

I have been able to add items using Canvas.Children.Add() , but I can't figure out how to create a binding to the Top and Left values.我已经能够使用Canvas.Children.Add()添加项目,但我不知道如何创建与TopLeft值的绑定。 Would it be a better idea to somehow bind the contents of the Canvas to a list and create all the bindings in XAML?以某种方式将Canvas的内容绑定到列表并在 XAML 中创建所有绑定会更好吗? Then again, how would I do that?再说一次,我该怎么做?

If you don't know the bindings in UWP, you can see this document .如果您不知道 UWP 中的绑定,可以查看此文档

Depending on your situation, you can consider using MVVM for binding, creating a ViewModel in Code-Behind and using Binding in the xaml to bind related properties.根据您的情况,您可以考虑使用 MVVM 进行绑定,在 Code-Behind 中创建ViewModel并使用 xaml 中的Binding来绑定相关属性。

CanvasPageViewModel.cs CanvasPageViewModel.cs

public class CanvasPageViewModel : INotifyPropertyChanged
{
    private double _rectTop;
    public double RectTop
    {
        get => _rectTop;
        set
        {
            _rectTop = value;
            OnPropertyChanged();
        }
    }
    private double _rectLeft;
    public double RectLeft
    {
        get => _rectLeft;
        set
        {
            _rectLeft = value;
            OnPropertyChanged();
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName]string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

CanvasPage.xaml.cs CanvasPage.xaml.cs

public CanvasPageViewModel viewModel = new CanvasPageViewModel();
public CanvasPage()
{
    this.InitializeComponent();
    this.DataContext = viewModel;
    viewModel.RectTop = 20;
    viewModel.RectLeft = 100;
}

CanvasPage.xaml CanvasPage.xaml

<Grid>
    <Canvas Width="500" Height="500" Background="White">
        <Rectangle Width="50" Height="50" Fill="Blue"
                   Canvas.Top="{Binding RectTop}"
                   Canvas.Left="{Binding RectLeft}"/>
    </Canvas>
</Grid>

This is a simple example.这是一个简单的例子。 If you want to modify the position of the control later, you can directly modify the data source and the UI will synchronize.如果以后想修改控件的position,可以直接修改数据源,UI会同步。


Update更新

If you need to manually add child elements of Canvas, you can use this method:如果需要手动添加Canvas的子元素,可以使用这种方法:

var myRect = new Rectangle() 
{ 
    Height = 50, 
    Width = 100, 
    Fill = new SolidColorBrush(Colors.Blue)
};

myCanvas.Children.Add(myRect);

But if you want to bind the created Rectangle element, as you said, bind the Canvas.Left property, you can write:但是如果要绑定创建的 Rectangle 元素,如你所说,绑定Canvas.Left属性,可以这样写:

public CanvasPageViewModel viewModel = new CanvasPageViewModel();
public CanvasPage()
{
    this.InitializeComponent();
    var myRect = new Rectangle() 
    { 
        Height = 50, 
        Width = 100, 
        Fill = new SolidColorBrush(Colors.Blue) 
    };

    Binding leftBinding = new Binding() 
    { 
        Path = new PropertyPath("RectLeft"),
        Mode=BindingMode.OneWay 
    };
    myRect.SetBinding(Canvas.LeftProperty, leftBinding);
    myCanvas.Children.Add(myRect);
}

Best regards.此致。

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

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