c# - 使用 WPF 和 XAML 以及数据绑定(bind)查看设计 View 中的 UI 更改?

标签 c# wpf xaml designview

我刚刚在 XAML 上观看了这个视频,他在其中创建了一个时钟,并注意到他实际上可以在 Xaml 设计 View 中看到他在 C Sharp 中所做的所有更改。

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

在 37:10,他绑定(bind)到那个类:https://youtu.be/Wb-l0e6WYE0?t=2227

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

我试图通过创建一个名为 Ball 的类来实现这一点,该类具有一些属性(例如大小),并将这些属性绑定(bind)到一个带有矩形的 Canvas,该矩形具有一个 EllipseGeometry 剪辑,使其呈圆形。运行应用程序时它工作正常,但设计 View 只是白色。

有人知道他是怎么做到的吗?

我的代码:

主窗口.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 找到它要绑定(bind)到的类的实例。

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

有两个独立的 DataContext:设计时运行时

要设置设计时 DataContext,请参阅:

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

本质上,当您设置设计时 DataContext 时,WPF 运行时会在幕后自动实例化您指向的类的新实例(如果它是静态的,则简单地指向该类),然后 Visual Studio 设计当您编辑 XAML 时,时间设计器将显示您的类中的实时值。这使得设计速度非常快,因为您可以使用实时数据,而且您不必一直运行程序来查看它的外观。

要设置运行时 DataContext,请参阅ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext" .答案描述了如何使用免费的 Snoop 实用程序来检测运行时绑定(bind)错误。

关于c# - 使用 WPF 和 XAML 以及数据绑定(bind)查看设计 View 中的 UI 更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539396/

相关文章:

c# - 我如何在新的 coreclr 世界中模拟对象?

wpf - IntegerUpDown 绑定(bind)问题

c# - 在 WPF 中关闭没有代码隐藏的窗口

c# - MVVM 绑定(bind)在设计时不起作用

wpf - 使用值转换器生成 GUI 友好的字符串是否是对值转换器的滥用?

c# - 在多个驱动程序上运行 WebDriver NUnit 测试

c# 迭代父类型的数组以调用派生类型的非多态方法

c# - WPF 交互触发 MouseDoubleClick 事件未触发

javascript - 为什么我的 C# 函数不从 ajax Javascript 代码执行?

c# - 我应该在 Prism 的什么地方使用 `RegisterViewWithRegion`?