英文:
How can I transform the location of a UserControl set on an Items Control?
问题
以下是代码部分的翻译:
UserControl 代码部分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace CADViewer.User_Controls
{
/// <summary>
/// 与 SignificantPointControl.xaml 交互逻辑
/// </summary>
[Serializable]
public partial class SignificantPointControl : System.Windows.Controls.UserControl, INotifyPropertyChanged
{
private Point dxfCenter;
private Point mapPageCanvasCenter;
private string color = "Red";
public Double Size { get; set; } = 2;
public Double Left { get; set; }
public Double Top { get; set; }
public Thickness Margin { get; set; }
public String Color
{
get { return color; }
set
{
color = value;
OnPropertyChanged();
}
}
public Point DxfCenter
{
get { return dxfCenter; }
set
{
dxfCenter = value;
OnPropertyChanged();
}
}
public Point MapPageCanvasCenter
{
get { return mapPageCanvasCenter; }
set
{
mapPageCanvasCenter = value;
OnPropertyChanged();
}
}
public SignificantPointControl()
{
DataContext = this;
InitializeComponent();
//DxfCenter = dxfCenter;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void GetDxfCenter(Point dxfCenter)
{
DxfCenter = dxfCenter;
}
public void GetMapPageCanvasCenter(List<double> extents, double canvasWidth, double canvasHeight)
{
MapPageCanvasCenter = DXFToCanvas.DXFCoordToCanvas(DxfCenter, extents, canvasWidth, canvasHeight);
Left = mapPageCanvasCenter.X - (Size / 2);
Top = mapPageCanvasCenter.Y - (Size / 2);
Margin = new Thickness(Left, Top, 0, 0);
}
}
}
UserControl XAML 部分:
<UserControl x:Class="CADViewer.User_Controls.SignificantPointControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CADViewer.User_Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Canvas x:Name="PointCanvas" Height="6" Width="6" Background="Transparent" Canvas.Left="{Binding MapPageCanvasCenter.X}" Canvas.Top="{Binding MapPageCanvasCenter.Y}">
<Ellipse Width="{Binding Size}" Height="{Binding Size}" Stroke="{Binding Color}" Canvas.Left="0" Canvas.Top="0"/>
</Canvas>
</UserControl>
ItemsControl 部分:
<ItemsControl x:Name="MapPageItemsControl"
Width="900" Height="500" MouseLeftButtonDown="MapPageItemsControl_MouseLeftButtonDown">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Transparent" Width="900" Height="500"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type uc:SignificantPointControl}">
<uc:SignificantPointControl.RenderTransform>
<TranslateTransform X="{Binding Left}" Y="{Binding Top}"/>
</uc:SignificantPointControl.RenderTransform>
</uc:SignificantPointControl>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.RenderTransform>
<MatrixTransform x:Name="transform"/>
</ItemsControl.RenderTransform>
</ItemsControl>
以上为您提供的代码的翻译。如果您有任何其他问题或需要进一步的帮助,请随时提出。
英文:
I've got an ItemsControl and I'm attempting to set a UserControl in a specific location on this particular ItemsControl. It is binding fine and is clearly on the ItemsControl but it is stuck at 0, 0. What am I missing here?
Here is the UserControl code behind:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace CADViewer.User_Controls
{
/// <summary>
/// Interaction logic for SignificantPointControl.xaml
/// </summary>
[Serializable]
public partial class SignificantPointControl : System.Windows.Controls.UserControl, INotifyPropertyChanged
{
private Point dxfCenter;
private Point mapPageCanvasCenter;
private string color = "Red";
public Double Size { get; set; } = 2;
public Double Left { get; set; }
public Double Top { get; set; }
public Thickness Margin { get; set; }
public String Color
{
get { return color; }
set
{
color = value;
OnPropertyChanged();
}
}
public Point DxfCenter
{
get { return dxfCenter; }
set
{
dxfCenter = value;
OnPropertyChanged();
}
}
public Point MapPageCanvasCenter
{
get { return mapPageCanvasCenter; }
set
{
mapPageCanvasCenter = value;
OnPropertyChanged();
}
}
public SignificantPointControl()
{
DataContext = this;
InitializeComponent();
//DxfCenter = dxfCenter;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void GetDxfCenter(Point dxfCenter)
{
DxfCenter = dxfCenter;
}
public void GetMapPageCanvasCenter(List<double> extents, double canvasWidth, double canvasHeight)
{
MapPageCanvasCenter = DXFToCanvas.DXFCoordToCanvas(DxfCenter, extents, canvasWidth, canvasHeight);
Left = mapPageCanvasCenter.X - (Size / 2);
Top = mapPageCanvasCenter.Y - (Size / 2);
Margin = new Thickness(Left, Top, 0, 0);
}
}
}
Here is the UserControl XAML:
<UserControl x:Class="CADViewer.User_Controls.SignificantPointControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CADViewer.User_Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Canvas x:Name="PointCanvas" Height="6" Width="6" Background="Transparent" Canvas.Left="{Binding MapPageCanvasCenter.X}" Canvas.Top="{Binding MapPageCanvasCenter.Y}">
<Ellipse Width="{Binding Size}" Height="{Binding Size}" Stroke="{Binding Color}" Canvas.Left="0" Canvas.Top="0"/>
</Canvas>
</UserControl>
And here is the ItemsControl:
<ItemsControl x:Name="MapPageItemsControl"
Width="900" Height="500" MouseLeftButtonDown="MapPageItemsControl_MouseLeftButtonDown" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Transparent" Width="900" Height="500"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type uc:SignificantPointControl}">
<uc:SignificantPointControl.RenderTransform>
<TranslateTransform X="{Binding Left}" Y="{Binding Top}"/>
</uc:SignificantPointControl.RenderTransform>
</uc:SignificantPointControl>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.RenderTransform>
<MatrixTransform x:Name="transform"/>
</ItemsControl.RenderTransform>
</ItemsControl>
I attempted to TranslateTransform to the correct location. I tried setting the margin. I also checked my bindings in the transform and it is correct but the UserControl does not move nonetheless.
答案1
得分: 0
你应该使用itemcontainerstyle
来定位你的椭圆:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
<Setter Property="Canvas.Left" Value="{Binding X}"/>
</Style>
</ItemsControl.ItemContainerStyle>
假设"significant point"表示你希望椭圆位于给定点的中心,那么你可以使用路径和ellipsegeometry
:
<Path
Height="7"
Width="7"
Fill="White"
Stroke="Black"
StrokeThickness="1">
<Path.Data>
<EllipseGeometry
RadiusX="2.5"
RadiusY="2.5" />
</Path.Data>
</Path>
这将位于其canvas.top
和canvas.left
点的中心。
我在开发游戏时使用上述方法来检查和可视化计算出的点。
该路径是我代码库中SpotView用户控件的内容。
在设计器中它看起来像这样:
你看到的蓝线是用户控件的边缘。
演示中的点本身位于用户控件的左上角,并延伸出通常被认为是用户控件边界框的部分。
<UserControl x:Class="UILib.SpotView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UILib"
mc:Ignorable="d" >
<Path
Height="7"
Width="7"
Fill="White"
Stroke="Black"
StrokeThickness="1">
<Path.Data>
<EllipseGeometry
RadiusX="2.5"
RadiusY="2.5" />
</Path.Data>
</Path>
</UserControl>
英文:
You should use itemcontainerstyle to position your ellipses:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
<Setter Property="Canvas.Left"Value="{Binding X}"/>
</Style>
</ItemsControl.ItemContainerStyle>
Assuming significant point means you want your ellipse centred about a given point then you can use a path and ellipsegeometry.
This:
<Path
Height="7"
Width="7"
Fill="White"
Stroke="Black"
StrokeThickness="1">
<Path.Data>
<EllipseGeometry
RadiusX="2.5"
RadiusY="2.5"
/>
</Path.Data>
</Path>
Will be centred on it's canvas.top and canvas.left point.
I use the above to check when I'm developing our game and I need to visualise calculated points.
That path is the content of a SpotView usercontrol in my code base.
In the designer it looks like:
The blue lines you see are the usercontrol edges.
Demonstrating the spot itself is centred around the usercontrol top left corner and extending "out" what you might usually consider to be the usercontrol bounding box.
<UserControl x:Class="UILib.SpotView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UILib"
mc:Ignorable="d"
>
<Path
Height="7"
Width="7"
Fill="White"
Stroke="Black"
StrokeThickness="1">
<Path.Data>
<EllipseGeometry
RadiusX="2.5"
RadiusY="2.5"
/>
</Path.Data>
</Path>
</UserControl>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论