Windows phone 7 绑定画布X,Y(当用作项控件项spanel时)
我有一个画布,我想在指定的XY位置绑定一些点。但是,我无法使用ItemsControl实现这一点。我找到了一些解决方案,但我想它们不适用于Windows Phone 我使用的XAML是:Windows phone 7 绑定画布X,Y(当用作项控件项spanel时),windows-phone-7,canvas,Windows Phone 7,Canvas,我有一个画布,我想在指定的XY位置绑定一些点。但是,我无法使用ItemsControl实现这一点。我找到了一些解决方案,但我想它们不适用于Windows Phone 我使用的XAML是: <ItemsControl ItemsSource="{Binding Path=Nodes}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas />
<ItemsControl ItemsSource="{Binding Path=Nodes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=XPos}" />
<Setter Property="Canvas.Top" Value="{Binding Path=YPos}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
我得到:“成员“ItemContainerStyle”未被识别或无法访问”
如何以其他方式进行这种绑定?我有对象的名称和X/Y值,我想把它绑定成某种图钉,最后我用C#完成了,只使用foreach
foreach (var InsidePoints in _inside)
{
double left = InsidePoints.xaxis * Layer_3.Width;
double top = InsidePoints.yaxis * Layer_3.Height;
Image pinImage = new Image();
pinImage.Name = InsidePoints.id.ToString();
pinImage.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("icons/inside_pin.png");
pinImage.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(pinImage_Tap);
//pinImage.Hold += new EventHandler<System.Windows.Input.GestureEventArgs>(pinImage_Hold);
Layer_3.Children.Add(pinImage);
Canvas.SetLeft(pinImage, left);
Canvas.SetTop(pinImage, top);
}
foreach(var InsidePoints in _inside)
{
左双=内点x轴*层3.宽度;
双层顶部=内部点。yaxis*层3.高度;
Image pinImage=新图像();
pinImage.Name=InsidePoints.id.ToString();
pinImage.Source=(ImageSource)new ImageSourceConverter().ConvertFromString(“icons/inside_pin.png”);
pinImage.Tap+=新事件处理程序(pinImage\u Tap);
//pinImage.Hold+=新的事件处理程序(pinImage\u Hold);
层_3.子对象。添加(pinImage);
Canvas.SetLeft(pinImage,左);
Canvas.SetTop(pinImage,top);
}
我遇到了同样的问题,但改用TriggerAction解决了它。您可以使用System.Windows.Interactivity
,如果您有。dll位于中
c:\Program Files\Microsoft SDKs\Expression\Blend\Silverlight\v4.0\Libraries\
System.Windows.Interactivity.dll`
然后,通过使用以前的xaml代码,我可以将datatemplate设置为:
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ellipse Stroke="Red" Width="2" Height="2">
<ia:Interaction.Triggers>
<ia:EventTrigger EventName="Loaded">
<tr:SetCanvasPropertiesAction Left="{Binding X}" Top="{Binding Y}" />
</ia:EventTrigger>
</ia:Interaction.Triggers>
</Ellipse>
</DataTemplate>
</ItemsControl.ItemTemplate>
在xaml文件的顶部
tr前缀用于包含我自己的类,如下所示:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
namespace Presentation.Triggers {
public class SetCanvasPropertiesAction : TriggerAction<DependencyObject> {
public static readonly DependencyProperty LeftProperty =
DependencyProperty.Register("Left", typeof(double), typeof(SetCanvasPropertiesAction), new PropertyMetadata(default(double)));
public static readonly DependencyProperty TopProperty =
DependencyProperty.Register("Top", typeof(double), typeof(SetCanvasPropertiesAction), new PropertyMetadata(default(double)));
public double Top {
get { return (double)GetValue(TopProperty); }
set { SetValue(TopProperty, value); }
}
public double Left {
get { return (double)GetValue(LeftProperty); }
set { SetValue(LeftProperty, value); }
}
protected override void Invoke(object parameter) {
UIElement presenter = (UIElement)VisualTreeHelper.GetParent(AssociatedObject);
Canvas.SetLeft(presenter, Left);
Canvas.SetTop(presenter, Top);
}
}
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Interactive;
使用System.Windows.Media;
名称空间表示.触发器{
公共类SetCanvasPropertiesAction:TriggerAction{
公共静态只读DependencyProperty LeftProperty=
Register(“Left”、typeof(double)、typeof(SetCanvasPropertiesAction)、newPropertyMetadata(default(double));
公共静态只读从属属性TopProperty=
Register(“Top”、typeof(double)、typeof(SetCanvasPropertiesAction)、newPropertyMetadata(default(double));
公共双层车顶{
获取{return(double)GetValue(TopProperty);}
set{SetValue(TopProperty,value);}
}
公共双左{
获取{return(double)GetValue(LeftProperty);}
set{SetValue(LeftProperty,value);}
}
受保护的覆盖无效调用(对象参数){
UIElement presenter=(UIElement)VisualTreeHelper.GetParent(AssociatedObject);
Canvas.SetLeft(演示者,左侧);
Canvas.SetTop(演示者,顶部);
}
}
}
Invoke方法需要注意两件事。第一个是AssociatedObject
,它被解析为椭圆,因为触发器嵌套在它下面的xaml中。第二件事是VisualTreeHelper,它获取椭圆的父对象。这是要在其上设置画布附加属性的ContentPresenter
它看起来可能更复杂,但与mvvm中的其他内容一样,您可以在xaml中重用它,而不必在任何地方复制和粘贴代码
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
namespace Presentation.Triggers {
public class SetCanvasPropertiesAction : TriggerAction<DependencyObject> {
public static readonly DependencyProperty LeftProperty =
DependencyProperty.Register("Left", typeof(double), typeof(SetCanvasPropertiesAction), new PropertyMetadata(default(double)));
public static readonly DependencyProperty TopProperty =
DependencyProperty.Register("Top", typeof(double), typeof(SetCanvasPropertiesAction), new PropertyMetadata(default(double)));
public double Top {
get { return (double)GetValue(TopProperty); }
set { SetValue(TopProperty, value); }
}
public double Left {
get { return (double)GetValue(LeftProperty); }
set { SetValue(LeftProperty, value); }
}
protected override void Invoke(object parameter) {
UIElement presenter = (UIElement)VisualTreeHelper.GetParent(AssociatedObject);
Canvas.SetLeft(presenter, Left);
Canvas.SetTop(presenter, Top);
}
}
}