use*_*280 6 c# wpf position canvas aspect-ratio
我试图在Canvas我的背景中定位元素。
窗口重新调整大小以保持纵横比。背景随着窗口大小而拉伸。
问题是一旦窗口重新调整大小,元素位置就不正确。如果窗口稍微调整大小,元素会稍微调整它们的大小并且仍然在正确的位置,但如果窗口调整大小使其大小加倍,则定位完全关闭。
到目前为止,我使用了Grid,但也无济于事。这是 XAML
<Window x:Class="CanvasTEMP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" ResizeMode="CanResizeWithGrip" SizeToContent="WidthAndHeight" MinHeight="386" MinWidth="397.5" Name="MainWindow1"
xmlns:c="clr-namespace:CanvasTEMP" Loaded="onLoad" WindowStartupLocation="CenterScreen" Height="386" Width="397.5" WindowStyle="None" AllowsTransparency="True" Topmost="True" Opacity="0.65">
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas Height="77" Width="218">
<Label Content="{Binding OwnerData.OwnerName}" Height="36" Canvas.Left="8" Canvas.Top="55" Width="198" Padding="0" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalAlignment="Center"/>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas>
<Canvas.Background>
<ImageBrush ImageSource="Resources\default_mapping.png" Stretch="Uniform"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding OwnerData.left}" />
<Setter Property="Canvas.Top" Value="{Binding OwnerData.top}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
用于数据绑定的类
public class Owner : INotifyPropertyChanged
{
public double _left;
public double _top;
public string OwnerName { get; set; }
public double top { get { return _top; }
set
{
if (value != _top)
{
_top = value;
OnPropertyChanged();
}
}
}
public double left
{
get { return _left; }
set
{
if (value != _left)
{
_left = value;
OnPropertyChanged();
}
}
}
public string icon { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "none passed")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ForDisplay
{
public Owner OwnerData { get; set; }
public int Credit { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是每秒运行的代码,以保持元素相对于窗口大小的位置
items[0].OwnerData.left = this.Width * (10 / Defaul_WindowSize_Width);
items[0].OwnerData.top = this.Height * (55 / Defaul_WindowSize_Height);
Run Code Online (Sandbox Code Playgroud)
10和50是默认Canvas.Left和Canvas.Top当第一次初始化窗口被使用。
如果有人能指出我做错了什么,将不胜感激。
虽然这篇文章很旧,但它仍然有帮助,所以这是我的答案。
我想出了两种方法来保持 a 中元素的相对位置Canvas:
这个想法是在 [0,1] 范围内提供两个值 (x,y),它们将定义元素相对于 Canvas 左上角的相对位置。这些 (x,y) 值将用于计算和设置正确的Canvas.Left和Canvas.Top值。
为了放置中心的元素的相对位置,我们将需要ActualWidth和ActualHeight的Canvas 和元素。
这是我对类似问题的完整回答,并附有示例和解释:
和听到的是它的外观:
本Ellipse是一个孩子Canvas保持相对位置的Canvas(和Image)。

| 归档时间: |
|
| 查看次数: |
2123 次 |
| 最近记录: |