- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 WPF 创建图表设计器并使用 MVVM 模式,我从本指南中获取信息和一些提示:https://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part-1
在一瞬间,我的项目看起来像:
WPF: Hydrate Canvas with Draggable Controls at Runtime
当然,我也遇到了和上面提到的作者类似的问题:当我画我的 ContentControl
时,它使用随机坐标正确绘制,但是当我尝试移动它时,它不会移动!当我调试类MoveThumb
,我看到我的 ContentControl
还没有得到它的Parent
.但在我看来,它应该有Canvas
作为 Parent
.我知道我应该重写一些系统/基本方法,但我不明白我应该重写什么以及如何重写它。也许有人有想法?
现在我尝试描述我的实现,首先我创建 BaseShapeViewModel
abstract public class BaseShapeViewModel : BaseViewModel
{
public BaseShapeViewModel()
{
}
private double left;
public double Left
{
get => left;
set => SetField(ref left, value, nameof(Left));
}
private double top;
public double Top
{
get => top;
set => SetField(ref top, value, nameof(Top));
}
private int width;
public int Width
{
get => width;
set => SetField(ref width, value, nameof(Width));
}
private int height;
public int Height
{
get => height;
set => SetField(ref height, value, nameof(Height));
}
private string fill;
public string Fill
{
get => fill;
set => SetField(ref fill, value, nameof(Fill));
}
private string text;
public string Text
{
get => text;
set => SetField(ref text, value, nameof(Text));
}
}
其他 ViewModel 的 EllipseViewModel、RectangleViewModel 继承自 BaseShapeViewModel。
class MainViewModel : BaseViewModel
{
public MainViewModel()
{
BaseShapeViewModels = new ObservableCollection<BaseShapeViewModel>();
}
public ObservableCollection<BaseShapeViewModel> BaseShapeViewModels { get; set; }
//public Canvas DesignerCanvas;
private RelayCommand createUseCase;
private RelayCommand createRectangle;
public ICommand CreateUseCase
{
get
{
return createUseCase ??
(
createUseCase = new RelayCommand(() => { AddUseCase(); })
);
}
}
public ICommand CreateRectangle
{
get
{
return createRectangle ??
(
createRectangle = new RelayCommand(() => { AddRectangle(); })
);
}
}
private void AddUseCase()
{
Random rnd = new Random();
int valueLeft = rnd.Next(0, 200);
int valueTop = rnd.Next(0, 200);
EllipseViewModel useCaseViewModel = new EllipseViewModel {Left=valueLeft,Top=valueTop, Height = 100, Width = 200, Fill="Blue"};
BaseShapeViewModels.Add(useCaseViewModel);
}
private void AddRectangle()
{
Random rnd = new Random();
int valueLeft = rnd.Next(0, 200);
int valueTop = rnd.Next(0, 200);
RectangleViewModel rectangleViewModel = new RectangleViewModel { Left = valueLeft, Top = valueTop, Height = 100, Width = 200, Fill = "Blue" };
BaseShapeViewModels.Add(rectangleViewModel);
}
}
我的 MoveThumb.cs 看起来像
public class MoveThumb : Thumb
{
public MoveThumb()
{
DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta);
}
private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
ContentControl designerItem = DataContext as ContentControl;
if (designerItem != null)
{
Point dragDelta = new Point(e.HorizontalChange, e.VerticalChange);
RotateTransform rotateTransform = designerItem.RenderTransform as RotateTransform;
if (rotateTransform != null)
{
dragDelta = rotateTransform.Transform(dragDelta);
}
double left = Canvas.GetLeft(designerItem);
double top = Canvas.GetTop(designerItem);
Canvas.SetLeft(designerItem, left + dragDelta.X);
Canvas.SetTop(designerItem, top + dragDelta.Y);
}
}
}
并且知道我想说我不是 Xaml 的专业人士,但我从第一个链接检查 Material 并像这样创建 MoveThumb.xaml
<ResourceDictionary
<ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MoveThumb.xaml"/>
<ResourceDictionary Source="ResizeDecorator.xaml"/>
<ResourceDictionary Source="RotateDecorator.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- ContentControl style to move, resize and rotate items -->
<Style x:Key="DesignerItemStyle" TargetType="ContentControl">
<Setter Property="MinHeight" Value="50"/>
<Setter Property="MinWidth" Value="50"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Control Name="RotateDecorator"
Template="{StaticResource RotateDecoratorTemplate}"
Visibility="Collapsed"/>
<s:MoveThumb Template="{StaticResource MoveThumbTemplate}"
Cursor="SizeAll"/>
<Control x:Name="ResizeDecorator"
Template="{StaticResource ResizeDecoratorTemplate}"
Visibility="Collapsed"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter TargetName="ResizeDecorator" Property="Visibility" Value="Visible"/>
<Setter TargetName="RotateDecorator" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我尝试在我的 MainWindow.xaml 中为我的 ContentControls 绑定(bind)此样式
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/DesignerItem.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Background="Gray" Grid.RowSpan="2">
<TextBlock Text="Shapes"
FontSize="18"
TextAlignment="Center" />
<Button Content="Initial Node" />
<Button Content="Final Node" />
<Button Content="Line" />
<Button Content="Action" />
<Button Content="Decision Node" />
<Button Content="Actor" />
<Button Content="Class" Command="{Binding Path=CreateRectangle}"/>
<Button Content="Use Case" Command="{Binding Path = CreateUseCase}"/>
</StackPanel>
<Grid Grid.Column="2">
<ItemsControl ItemsSource="{Binding Path= BaseShapeViewModels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type viewModel:EllipseViewModel}">
<ContentControl
Selector.IsSelected="True"
Style="{StaticResource DesignerItemStyle}">
<Ellipse
Fill="{Binding Fill}"
IsHitTestVisible="False"/>
</ContentControl>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:RectangleViewModel}">
<ContentControl
Selector.IsSelected="True"
Style="{StaticResource DesignerItemStyle}">
<Rectangle
Fill="{Binding Fill}"
IsHitTestVisible="False"/>
</ContentControl>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Left, Mode=TwoWay}"/>
<Setter Property="Canvas.Top" Value="{Binding Top, Mode=TwoWay}"/>
<Setter Property="Width" Value="{Binding Width, Mode=TwoWay}"/>
<Setter Property="Height" Value="{Binding Height,Mode=TwoWay}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Grid>
最佳答案
您不能将项目拖过 Canvas
, 因为您使用的是 ItemsControl
.此控件将每个项目包装到一个容器中。您当前正在尝试拖动此容器的内容,而不是容器。它是实际位于 Canvas
内的容器。 .
为了降低复杂性,我建议实现一个扩展 ItemsControl
的自定义控件。 .这样,您可以使项目容器本身可拖动。或者,您可以实现附加行为。
要提供调整大小和旋转用户界面(例如调整夹点大小),我建议使用 Adorner
.
下面的例子实现了一个自定义项容器,它支持拖动、缩放和旋转,以及一个扩展的ItemsControl
将这个容器用于其项目。
由于可拖动元素是 ContentControl
,它不需要任何额外的包装。这将简化使用,如下所示。
实现 DraggableContentControl
DraggableContentControl.cs DraggableContentControl
类用作 ItemsCanvas
的项目容器控制(专门的 ItemsControl
- 见下文)。
此外,DraggableContentControl
可用作独立控件:DraggableContentControl
是一种非常方便且可重复使用的方法,可以将鼠标拖动、缩放和旋转添加到任何 UIElement
在 Canvas
.例如允许拖动 Rectangle
横跨Canvas
只需分配 Rectangle
到DraggableContentControl.Content
DraggableContentControl
的属性和位置在 Canvas
:
<Canvas Width="1000" Height="1000">
<!-- Ad mouse drag, rotation and scaling to a Rectangle -->
<DraggableContentControl Canvas.Left="50"
Canvas.Top="50"
Angle="45">
<Rectangle Height="100"
Width="100"
Fill="Coral" />
</DraggableContentControl>
</Canvas>
旋转 :设置
DraggableContentControl.Angle
允许旋转托管元素。
DraggableContentControl.Width
和
DraggableContentControl.Height
允许缩放/调整内容的大小(自动,因为
DraggableContentControl.Content
值被包装到
Viewbox
- 请参阅下面的默认
Style
)。
Auto
,
DraggableContentControl
将动态采用其内容的大小。
public class DraggableContentControl : ContentControl
{
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register(
"Angle",
typeof(double),
typeof(DraggableContentControl),
new PropertyMetadata(default(double), DraggableContentControl.OnAngleChanged));
public double Angle
{
get => (double) GetValue(DraggableContentControl.AngleProperty);
set => SetValue(DraggableContentControl.AngleProperty, value);
}
private RotateTransform RotateTransform { get; set; }
private IInputElement ParentInputElement { get; set; }
private bool IsDragActive { get; set; }
private Point DragOffset { get; set; }
static DraggableContentControl()
{
// Remove this line if you don't plan to define the default Style
// inside the Generic.xaml file.
DefaultStyleKeyProperty.OverrideMetadata(typeof(DraggableContentControl), new FrameworkPropertyMetadata(typeof(DraggableContentControl)));
}
public DraggableContentControl()
{
this.PreviewMouseLeftButtonDown += InitializeDrag_OnLeftMouseButtonDown;
this.PreviewMouseLeftButtonUp += CompleteDrag_OnLeftMouseButtonUp;
this.PreviewMouseMove += Drag_OnMouseMove;
this.RenderTransformOrigin = new Point(0.5, 0.5);
var transformGroup = new TransformGroup();
this.RotateTransform = new RotateTransform();
transformGroup.Children.Add(this.RotateTransform);
this.RenderTransform = transformGroup;
}
#region Overrides of FrameworkElement
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
// Parent is required to calculate the relative mouse coordinates.
DependencyObject parentControl = this.Parent;
if (parentControl == null
&& !TryFindParentElement(this, out parentControl)
&& !(parentControl is IInputElement))
{
return;
}
this.ParentInputElement = parentControl as IInputElement;
}
#endregion
private void InitializeDrag_OnLeftMouseButtonDown(object sender, MouseButtonEventArgs e)
{
// Do nothing if disabled
this.IsDragActive = this.IsEnabled;
if (!this.IsDragActive)
{
return;
}
Point relativeDragStartPosition = e.GetPosition(this.ParentInputElement);
// Calculate the drag offset to allow the content to be dragged
// relative to the clicked coordinates (instead of the top-left corner)
this.DragOffset = new Point(
relativeDragStartPosition.X - Canvas.GetLeft(this),
relativeDragStartPosition.Y - Canvas.GetTop(this));
// Prevent other controls from stealing mouse input while dragging
CaptureMouse();
}
private void CompleteDrag_OnLeftMouseButtonUp(object sender, MouseButtonEventArgs e)
{
this.IsDragActive = false;
ReleaseMouseCapture();
}
private void Drag_OnMouseMove(object sender, MouseEventArgs e)
{
if (!this.IsDragActive)
{
return;
}
Point currentPosition = e.GetPosition(this.ParentInputElement);
// Apply the drag offset to drag relative to the
// initial mouse down coordinates (instead of the top-left corner)
currentPosition.Offset(-this.DragOffset.X, -this.DragOffset.Y);
Canvas.SetLeft(this, currentPosition.X);
Canvas.SetTop(this, currentPosition.Y);
}
private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as DraggableContentControl).RotateTransform.Angle = (double) e.NewValue;
}
private bool TryFindParentElement<TParent>(DependencyObject child, out TParent resultElement)
where TParent : DependencyObject
{
resultElement = null;
if (child == null)
{
return false;
}
DependencyObject parentElement = VisualTreeHelper.GetParent(child);
if (parentElement is TParent)
{
resultElement = parentElement as TParent;
return true;
}
return TryFindParentElement(parentElement, out resultElement);
}
}
实现
ItemsCanvas
ItemsCanvas.cs
ItemsCanvas
是
ItemsControl
配置为使用
DraggableContentControl
作为元素容器。
class ItemsCanvas : ItemsControl
{
static ItemsCanvas()
{
// Remove this line if you don't plan to define the default Style
// inside the Generic.xaml file.
DefaultStyleKeyProperty.OverrideMetadata(typeof(ItemsCanvas), new FrameworkPropertyMetadata(typeof(ItemsCanvas)));
}
#region Overrides of ItemsControl
protected override bool IsItemItsOwnContainerOverride(object item) => item is DraggableContentControl;
protected override DependencyObject GetContainerForItemOverride() => new DraggableContentControl();
#endregion
}
Generic.xaml
DraggableContentControl
的默认样式和
ItemsCanvas
:
<Style TargetType="DraggableContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DraggableContentControl">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<!--
Optional: wrapping the content into a Viewbox
allows to automatically resize/scale the content
based on the container's (DraggableContentControl) size.
-->
<Viewbox Stretch="Fill">
<ContentPresenter />
</Viewbox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ItemsCanvas">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ItemsCanvas">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer>
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Window>
<Window.Resources>
<DataTemplate DataType="{x:Type RectangleViewModel}">
<Rectangle Height="{Binding Height}"
Width="{Binding Width}"
Fill="{Binding Fill}" />
</DataTemplate>
<DataTemplate DataType="{x:Type EllipseViewModel}">
<Ellipse Height="{Binding Height}"
Width="{Binding Width}"
Fill="{Binding Fill}" />
</DataTemplate>
</Window.Resources>
<ItemsCanvas ItemsSource="{Binding BaseShapeViewModels}"
Height="500"
Width="500">
<ItemsCanvas.ItemContainerStyle>
<!-- Optional Style that adds the possibility to position items on the Canvas
using the e.g., Top and Left properties of the data model.
-->
<Style TargetType="main:DraggableContentControl">
<Setter Property="Canvas.Left" Value="{Binding Left, Mode=TwoWay}" />
<Setter Property="Canvas.Top" Value="{Binding Top, Mode=TwoWay}" />
</Style>
</ItemsCanvas.ItemContainerStyle>
</ItemsCanvas>
</Window>
关于c# - 如何在 Canvas 上拖动移动、调整大小/缩放、旋转控制元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63612157/
我在 div 和 jquery ui slider 中有一个图像列表,当用户滑动栏时,图像应该调整大小/缩放(无论你想怎么调用它),我尝试选择所有图像并更改 css 的宽度和使用 jquery 的高度
我正在制作一张具有缩放和平移功能的世界地图。我在某些城市上画了圆圈,圆圈的半径由数据决定。当鼠标悬停在这些圆圈上时,将出现一个工具提示来显示数据。 代码结构为 //在此选择上调用缩放行为 - 让我们调
我正在使用 jquery UI slider (http://jqueryui.com/slider/)。 我需要对整个站点进行缩放。使用 css 样式在 IE+Chrome im 中缩放 - “缩放
我花了很多时间试图找到一种使用 CGAffineScale 将 View 转换为给定点的方法,包括摆弄 anchor 、在转换前后移动 View 的中心以及全面的谷歌搜索。我知道使用 UIScroll
我希望能够用手指旋转和缩放/缩放我的位图,我已经为此寻找了所有示例项目,但它们都与我的代码不匹配。这是我的 onTouchListener 代码。 public class MyView extend
在我的页面中间,我有一个 div 元素,其中包含一些内容(其他 div、图像等)。 before something inside another something
我一直在尝试使用 google maps API V3 的绘图管理器(绘制矩形)。使用 -webkit-transform 缩放谷歌地图 Canvas (包含 div)后像这样 var transf
这个问题在这里已经有了答案: How to resize the iPhone/iPad Simulator? (12 个答案) 关闭 5 年前。 我正在编写 iOS 应用 我使用 Xcode 6.
这几天一直在研究微服务,我想知道人们是如何着手自动化负载平衡和扩展这些东西的? 我心中有一个特定的场景,我想实现什么,但不确定是否可行,或者我的想法有误。就这样吧…… 假设我有一个由 3 台名为 A、
我正在使用 ffmpeg for android 来制作 mp4 格式的视频。我无法让这个命令在 FFMPEG 中工作,基本上我正在尝试添加两个图像,缩放它们,添加缩放效果,最后将结果连接到一个视频文
使用 OpenGL 我正在尝试绘制我校园的原始 map 。 谁能向我解释一下平移、缩放和旋转通常是如何实现的? 例如,通过平移和缩放,这仅仅是我调整我的视口(viewport)吗?所以我绘制并绘制了构
我需要在 iphone sdk 界面生成器中将按钮旋转 30 度,该怎么做? 最佳答案 您无法在 Interface Builder 中执行此操作,但代码非常简单。 确保您已将 IB 中的按钮连接到
假设默认级别等于“1”,是否可以检测触摸设备的捏合(缩放)级别?原因是我希望根据捏合级别禁用页面元素(显示:无)。 如果可以将其放在一个设置缩放级别值的函数中,那就太好了,例如: var ZOOM =
我正在努力找出并确定如何根据这个例子放大我的鼠标位置。 (https://stackblitz.com/edit/js-fxnmkm?file=index.js) let node, scal
我已将 UIWebView 的“scalesPageToFit”属性设置为 YES。 它正在缩放,但是当页面加载时,内容的字体很小,不捏就无法阅读。我们可以将缩放比例设置为默认值吗? 最佳答案 这更多
我仍在学习 jQuery mobile 的技巧,并且在 data-role="page"上放大和缩小图片/图像时遇到问题。有没有办法使用 jquery mobile 在 iPhone 上的图像上进行捏
给定一组 n 个维度为 d 的向量,存储在 (n,d) 数组中,以及第二组 m 相同维度的向量(存储在 (m,d) 数组中)我想计算向量之间的平方点距离,由大小为 的某个矩阵 A 缩放>(d,d)。
我想知道是否有人可以指出我正确的方向。 我使用 CSS3 过渡创建了缩放效果。将鼠标悬停在该对象上,它会转换为更大的版本。这看起来很棒并且效果很棒,但问题是这种效果在 IE 中不起作用,所以我需要在
mapbox-gl-js 版本:0.38.0在 ionic 2.2.11 上通过 npm repo 使用 正如您在这张 gif 上看到的 https://giphy.com/gifs/ionic-an
所以我花了几个小时在网上搜索帮助,但所有论坛和文档都没有向我正确解释我应该采取的最佳方式。 目前我有一个具有 XML 相对布局的 Activity (背景设置为 map 图片),我想实现多手势缩放功能
我是一名优秀的程序员,十分优秀!