【发布时间】:2015-01-15 17:39:47
【问题描述】:
按照my previous question,我现在可以通过行为在画布中拖动项目。
问题是项目新坐标计算是在行为内部完成的,我不确定如何在视图中不写任何内容的情况下将它们发送到项目的 ViewModel。
我的 MainViewModel 有一个 Items 集合:
public BindableCollection<ItemViewModel> Items { get; set; }
ItemViewModel 看起来像这样:
[ImplementPropertyChanged]
public class ItemViewModel
{
private readonly IEventAggregator events;
private Random r = new Random();
public ItemViewModel(IEventAggregator events)
{
this.events = events;
this.events.Subscribe(this);
// default values
this.BackgroundColor = this.RandomColor();
this.Width = 100;
this.Height = 100;
this.X = 10;
this.Y = 10;
}
public Brush BackgroundColor { get; set; }
public string Content { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public double X { get; set; }
public double Y { get; set; }
/// <summary>
/// https://stackoverflow.com/a/11282427/6776
/// </summary>
private SolidColorBrush RandomColor()
{
var properties = typeof(Brushes).GetProperties();
var count = properties.Count();
var color = properties.Select(x => new { Property = x, Index = this.r.Next(count) })
.OrderBy(x => x.Index)
.First();
return (SolidColorBrush)color.Property.GetValue(color, null);
}
}
目前,我的 DragBehavior 只是 this code snippet 的复制/粘贴。
最后,我的 MainView 显示这样的项目(我正在使用 Caliburn.Micro 通过 x:Name 绑定项目):
<ItemsControl x:Name="Items">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=X}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
<Setter Property="Width" Value="{Binding Path=Width}" />
<Setter Property="Height" Value="{Binding Path=Height}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="{Binding Path=BackgroundColor}"
behaviors:DragBehavior.Drag="True">
<!-- contents -->
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我正在考虑添加一些类似的内容:
<Border ...
behaviors:DragBehavior.OnDropped="{Binding Dropped}">
在 ItemViewModel 中:
public void Dropped(Point newCoordinates) {
// change the X and Y of the viewmodel
}
但我不知道从哪里开始,甚至不知道该怎么称呼它。行动 ?扳机 ?它似乎与我要查找的内容无关。
Gong DragDropBehavior 库有一个系统,其中项目实现接口并且行为运行正确的方法,但我不太了解它是如何工作的(这是我第一次处理行为)。
【问题讨论】:
标签: c# wpf mvvm triggers action