有一个主窗口和一个画布类Graphcontext
,其中动画和几个形状互动如下:
<DockPanel Name="stackPanel2" DockPanel.Dock="Left" Margin="10,10,10,10" LastChildFill="True" >
<myctrl:Graphcontext x:Name="graphSurface" Background="Black" >
</myctrl:Graphcontext>
</DockPanel>
public class Graphcontext : Canvas
{
Ellipse _fixedCircle;
internal int CavasWidth { get; set; }
internal int CavasHeight { get; set; }
public void drawSinglePoint(SolidColorBrush color)
{
this.Children.Clear();
_fixedCircle = new Ellipse();
_fixedCircle.Width = 25;
_fixedCircle.Height = 25;
_fixedCircle.Stroke = color;
_fixedCircle.Fill = color;
_fixedCircle.StrokeThickness = 3;
// Get the center x and y coordinates
double x = this.ActualWidth / 2 ;
double y = this.ActualHeight / 2 ;
_fixedCircle.Margin = new Thickness(x, y, 1, 1);
// Add the circle to the canvas
this.Children.Add(_fixedCircle);
this.InvalidateVisual();
}
...
}
我希望克隆 GraphContext
在第二个其他监视器中的其他最大化画布内使用Viewbox。
我试过了
Canvas copycanvas = XamlReader.Parse(XamlWriter.Save(graphSurface)) as Canvas;
Viewbox vb = new Viewbox() { StretchDirection = StretchDirection.Both, Stretch=Stretch.Uniform };
vb.Child = copycanvas;
Window newwin = new Window() { Content = vb };
newwin.Show();
但是,当graphSurface
更新时,copycanvas
不会更新。
我实际上看到了这一点,但是当我做的时候,使用代码隐藏copycanvas
的故事板的动画不会更新。
我需要做什么copycanvas
始终是graphSurface
的镜像?
我是否需要将所有逻辑复制到其他控件中?这样它总是一样的,也许有点延迟......
也许数据绑定画布到视图框可以做到,但它会怎么样?
答案 0 :(得分:1)
您可以使用VisualBrush,Example1; Example2
基本上,您只需创建一个VisualBrush
并将当前GraphContext
数据绑定到其Visual
属性,这会创建GraphContext
的重复图像。
主窗口中的代码:
MirrorWindow newwin = new MirrorWindow ();
newwin.DataContext = graphSurface;
newwin.Show();
镜像窗口的XAML:
<Grid>
<Grid.Background>
<VisualBrush Stretch="Uniform" Visual="{Binding}"/>
</Grid.Background>
</Grid>
样品:
MainWindow在mousedown上有一个基本的Canvas和动画:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Canvas Background="Black" x:Name="graphSurface">
<Canvas.Triggers>
<EventTrigger RoutedEvent="MouseDown">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Red" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" FillBehavior="Stop" Duration="0:0:5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
</Canvas>
</Grid>
</Window>
镜像窗口:
<Window x:Class="WpfApplication1.Mirror"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Mirror" Height="300" Width="300">
<Grid>
<Grid.Background>
<VisualBrush Stretch="Fill" Visual="{Binding}"/>
</Grid.Background>
</Grid>
</Window>
MainWindow代码背后:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Mirror mm = new Mirror();
mm.DataContext = this.graphSurface;
mm.Show();
}
}
结果:
鼠标点击Mainwindow
,MainWindow
和Mirror
都会显示换色动画。
答案 1 :(得分:0)
当您在wpf中启动代码时,您应该停止处理过去编程语言之类的元素 你必须使用MVVM模式来做正确的事情 这是通过设置窗口的数据上下文来完成的 m-v-vm模式有事物
视图模型:
一个类实现带有一些公共属性的INotifyPropertyChanged,在设置时会引发事件属性 和初始化子女巫添加圆形位置,颜色等数据。
观点:
xaml女巫数据绑定到视图模型puplic属性与绑定mods你可以以一个&gt;方式(到目标),两个&lt; - &gt;方式,一个&lt; -way到源
绑定数据模特:
你的主窗口有初始化子像 InitializeComponent(); this.DataContext = New view_model.mainsub
所以在你的例子中你应该 - &gt;在view-model类中创建一些数据 - &gt;开始新窗口 - &gt;将窗口Datacontext设置为新的view_model
假设您想从列表当前项目
等视图中获取数据你应该将selected_index属性绑定到单向源模式,这样它就会改变在view_model类中绑定到它的相应属性
然后您可以检查主窗口(模型)中的属性是否已更改,以及MVVM的工作原理
请不要从视图中获取元素,您应该从视图中获取(view_model类)类型的数据
和wpf中的屏幕是仅用于发布数据不要扔掉孩子/父母关系到底你会被卡住