我正在尝试数据绑定到这个ItemsControl:
<ItemsControl ItemsSource="{Binding Path=Nodes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
通过使用它DataTemplate,我试图单独定位我的Node元素Canvas正确:
<DataTemplate DataType="{x:Type Model:EndNode}">
<Controls:EndNodeControl Canvas.Left="{Binding Path=XPos}" Canvas.Top="{Binding Path=YPos}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
但是,它没有按预期工作.我的所有节点元素都在相同位置绘制在彼此之上.有关如何实现这一目标的任何建议?
我正在使用ItemsControl,其中ItemsPanel设置为Canvas(有关更多背景信息,请参阅此问题).ItemsControl正在按我的意愿执行,当通过将子元素放入ItemsControl.Items手动添加子元素时,它就像一个魅力:
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Items>
<Button Canvas.Left="500" Content="Button Text" />
</ItemsControl.Items>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
注意Button上的Canvas.Left属性.这就像魅力一样,Button位于ItemsControl左侧500像素的位置.大!
但是,当我定义与List的ItemsSource绑定时,Canvas.left没有任何影响:
<ItemsControl ItemsSource="{Binding Elements}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Canvas.Left="500" Content="Button Text" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
通过在运行时检查应用程序,我看到了一个区别.在Canvas和按钮之间添加了容器ContentPresenter.
如何在ContentPresenter本身上设置Canvas.Left属性?或者有另一种方法可以解决这个问题吗?
谢谢大家!
我试图使用Canvas显示具有"世界"位置(而不是"屏幕"位置)的对象.画布定义如下:
<Canvas Background="AliceBlue">
<ItemsControl Name="myItemsControl" ItemsSource="{Binding MyItems}">
<Image x:Name="myMapImage" Panel.ZIndex="-1" />
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<TextBlock Canvas.Left="{Binding WorldX}" Canvas.Top="{Binding WorldY}"
Text="{Binding Text}"
Width="Auto" Height="Auto" Foreground="Red" />
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
MyItem的定义如下:
public class MyItem
{
public MyItem(double worldX, double worldY, string text)
{
WorldX = worldX;
WorldY = worldY;
Text = text;
}
public double WorldX { get; set; }
public double WorldY { get; set; }
public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
另外,我有一种在世界和屏幕坐标之间进行转换的方法:
Point worldToScreen(double worldX, double …Run Code Online (Sandbox Code Playgroud) 我的主窗口ViewModel有一个ViewModel的ObservableCollection,名为ViewModels.
Mainwindow XAML有一个ItemsControl,其ItemsSource绑定到ViewModels.
当我有
<ItemsControl ItemsSource="{Binding ViewModels}" />
Run Code Online (Sandbox Code Playgroud)
与集合中的每个ViewModel关联的视图将一个在另一个下方呈现.视图是UserControls,显示dataGrids.
如何以可自定义的方式定位它们,例如VM1位于左侧,VM2和VM3堆叠在VM1右侧的另一个之上.
每个VieModel都有PosX,PosY,Width和Height属性,我一直在尝试各种模板方法但到目前为止还没有成功.
我已经找到了如何使用Observable图像集合的示例,但我正在努力的一件事是我的集合是ViewModels.