我正在尝试将一个集合绑定到ItemsControl,将Canvas作为items面板,并将每个项目的Canvas.Left和Top绑定到项目对象上的属性.基本上我正在尝试重新创建我在博客上的这篇文章中描述的二维数据绑定,但这次是在WinRT而不是WPF.
由于ItemsControl将您的ItemTemplate内容包装在另一个UI元素(在WinRT的情况下为ContentPresenter)中,并且它是直接放置在items面板中的那些包装器/容器元素,因此必须在这些容器上设置Left和Top; 你不能只在DataTemplate中设置它们.在WPF中,使用ItemContainerStyle中的绑定很容易做到这一点,例如:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
<Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
但是当我在WinRT/XAML项目中尝试同样的事情时,我什么也得不到.甚至没有绑定错误.如果我对一个值进行硬编码,它就会起作用; 但是如果我使用绑定,则该属性将保持其默认值(零),并且"输出"窗口中不显示任何绑定错误.
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<!-- This works, so ItemContainerStyle does work in WinRT: -->
<Setter Property="Canvas.Left" Value="200"/>
<!-- But this silently fails, leaves Top as 0, and does not show
any binding errors in the debugger's Output window: -->
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
我已经验证了ContentPresenters确实有正确的DataContext(即集合项,而不是集合本身或其他时髦的东西),所以你认为这些绑定可以正常工作.但他们似乎甚至没有得到评估.如果我在其他地方放置了一个错误的绑定,并运行调试版本,我会在调试器的Output窗口中看到绑定错误; 但是如果我在ItemContainerStyle中引用了一个无意义的属性,则不会显示绑定错误.
这是一个更完整的例子,(据我所知)应该可以在WPF中正常工作,但是在WinRT中,所有内容都保留在原点:
<ItemsControl ItemsSource="{Binding Tiles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style …Run Code Online (Sandbox Code Playgroud) binding itemscontrol itemcontainerstyle windows-runtime winrt-xaml
我有一个由其边界点指定的可观察的线段集合.如何绑定它以在画布上绘制线条?
我已经看到了只使用一个点定义位置的形状解决方案.但是为了将这种方法应用于线条,它需要在坐标上进行笨拙的预计算以获得外部矩形的位置并使线条坐标相对于它.有没有办法避免它?
在GDI +/WinForms中,我可以在Click()事件中使用图形对象:
AddPoint(p); //Add Point contains some code to make sure there is only 3 dots
foreach (Point p in PointList) {
DrawRectangle(p);
}
Invalidate();
Run Code Online (Sandbox Code Playgroud)
如果我在WPF中尝试类似的东西,它将不会清理我创建的点(我猜是因为WPF的工作原理).这意味着如果我检查确保一次只有三个点,并弹出最旧的点为新的点腾出空间,绘制的矩形仍然存在.
所以问题是,如何在WPF中创建允许我的东西
我有这个xaml:
<Canvas cal:View.Context="DrawCanvas">
<!--<Line X1="1" Y1="1" X2="400" Y2="400" Stroke="Black" StrokeThickness="20" IsHitTestVisible="False"/>-->
</Canvas>
Run Code Online (Sandbox Code Playgroud)
在模型中我有:
public Canvas DrawCanvas { get; set; }
public ImageSourceViewModel()
{
this.PropertyChanged += this.ImageSourceViewModel_PropertyChanged;
this.Scale = 1;
this.TranslateX = 0;
this.TranslateY = 0;
DrawCanvas=new Canvas();
var line = new Line();
line.X1= 1;
line.Y1 = 1;
line.X2 = 100;
line.Y2 = 10;
line.Stroke=new SolidColorBrush(Colors.Green);
line.StrokeThickness = 2;
line.Visibility=Visibility.Visible;
DrawCanvas.Children.Add(line);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Caliburn Micro.
它不会在输出上绘制任何行.
这个问题可能有两个原因:
1-视图中的画布未绑定到ViewModel中的DrawCanvas.
2-绘图代码不正确.
如何检查我的视图画布是否实际绑定到ViewModel中的DrawCanvas?绑定的语法是否正确?我正在使用Caliburn Micro.
如果绑定是正确的,那么绘制代码的问题是什么呢?
wpf ×3
binding ×2
c# ×2
canvas ×1
data-binding ×1
drawing ×1
itemscontrol ×1
mvvm ×1
shapes ×1
winrt-xaml ×1