我只是在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的类来实现此目的,该类具有一些属性,例如大小,然后将这些属性绑定到具有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 time
和run 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实用程序来检测运行时绑定错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句