繁体   English   中英

在WPF和XAML和数据绑定的设计视图中查看UI更改吗?

[英]See UI changes in design view with WPF & XAML and data binding?

我只是在XAML上观看此视频,他在那里创建了一个时钟,并注意到他实际上可以在Xaml设计视图中看到他从C Sharp所做的所有更改。

在33:30,他创建了自己的课程: https ://youtu.be/Wb-l0e6WYE0?t = 2008

在37:10他绑定到该类: https ://youtu.be/Wb-l0e6WYE0?t=2227

稍后在40:17,您实际上可以在设计视图中看到时钟在滴答!

我试图通过创建一个名为Ball的类来实现此目的,该类具有一些属性,例如size,并将这些属性绑定到具有EllipseGeometry剪辑的矩形的Canvas上。 运行应用程序时,它工作正常,但设计视图仅为白色。

有谁知道他是怎么做到的?

我的代码:

MainWindow.xaml

<Window x:Class="XamlTest.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:XamlTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Canvas Background="White">
        <Rectangle Height="{Binding Size}" Width="{Binding Size}" Fill="Green" Canvas.Top="40">
            <Rectangle.Clip>
                <EllipseGeometry Center="{Binding EllipseCenter}" RadiusX="{Binding EllipseRadius}" RadiusY="{Binding EllipseRadius}"/>
            </Rectangle.Clip>
        </Rectangle>
    <Button x:Name="button" Content="Button" Width="75" Click="button_Click"/>
</Canvas>

背后的代码:

using System.Windows;
namespace XamlTest
{
public partial class MainWindow : Window
{
    Ball TheBall = new Ball();
    public MainWindow()
    {
        InitializeComponent();
        TheBall.Size = 300;
        this.DataContext = TheBall;
    }


    private void button_Click(object sender, RoutedEventArgs e)
    {

        TheBall.Size = TheBall.Size + 40;
    }
}
}

球类:

using System.Windows;
namespace XamlTest
{
class Ball : INotifyPropertyChangedBase
{
    public Ball()
    {
        Size = 50;
    }
    private double _size;
    public double Size
    {
        get
        {
            return _size;
        }
        set
        {
            _size = value;
            EllipseCenter = new Point(_size / 2, _size / 2);
            EllipseRadius = _size / 2;
            OnPropertyChanged("Size");
        }
    }

    private Point _ellipseCenter;
    public Point EllipseCenter
    {
        get
        {
            return _ellipseCenter;
        }
        set
        {

            _ellipseCenter = value;
            OnPropertyChanged("EllipseCenter");
        }
    }

    private double _ellipseRadius;
    public double EllipseRadius
    {
        get {
            return _ellipseRadius;
        }
        set {
            _ellipseRadius = value;
            OnPropertyChanged("EllipseRadius");
        }
    }


}
}

INotifyPropertyChangedBase类:

using System.ComponentModel;
namespace XamlTest
{

public class INotifyPropertyChangedBase : INotifyPropertyChanged
{


    public event PropertyChangedEventHandler PropertyChanged;

    internal void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
}

我也有一个增加球大小的按钮!

谢谢您的帮助。

DataContext允许XAML查找要绑定到的类的实例。

然后,XAML中的绑定允许您绑定到所述类的特定属性。

有两个单独的DataContext: design timerun time

要设置design time DataContext,请参见:

http://adamprescott.net/2012/09/12/design-time-data-binding-in-wpf/

本质上,当您设置设计时间DataContext时,WPF运行时将在后台自动实例化您指向它的类的新实例(或者如果指向静态,则简单地指向该类),然后Visual Studio设计时设计器将在编辑XAML时显示类中的实时值。 这样就可以真正快速地进行设计,因为您可以处理实时数据,而不必一直运行程序来查看其外观。

若要设置run time DataContext,请参阅ReSharper WPF错误:“由于未知的DataContext,无法解析符号“ MyVariable”” 答案描述了如何使用免费的Snoop实用程序来检测运行时绑定错误。

添加了以下代码:

d:DataContext="{d:DesignInstance  local:Ball,IsDesignTimeCreatable=True}"

现在,我可以在设计时看到我的绿球了!

谢谢!

暂无
暂无

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

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