Wpf 如何使线条停留在画布上的一个位置?

Wpf 如何使线条停留在画布上的一个位置?,wpf,canvas,resize,Wpf,Canvas,Resize,我在画布上画了两个椭圆,并在它们之间画了一条线。当窗口高度改变时,直线开始在Y坐标中移动。如何使直线保持在一个位置或使用直线移动椭圆 <Window x:Class="LineEllipse.MainWindow" Title="MainWindow" Height="768" Width="1366" WindowState="Maximized" SizeChanged="Window_SizeChanged"> <Window.DataContext>

我在画布上画了两个椭圆,并在它们之间画了一条线。当窗口高度改变时,直线开始在Y坐标中移动。如何使直线保持在一个位置或使用直线移动椭圆

<Window x:Class="LineEllipse.MainWindow"
    Title="MainWindow" Height="768" Width="1366" WindowState="Maximized" SizeChanged="Window_SizeChanged">

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
<Grid>
    <ItemsControl>
        <ItemsControl.Resources>
            <CollectionViewSource x:Key="Ellipses" Source="{Binding Ellipses}"/>
            <CollectionViewSource x:Key="Lines" Source="{Binding Lines}"/>
            <DataTemplate DataType="{x:Type local:BlackEllipse}">
                <Ellipse Width="26" Height="26" Fill="Black"/>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:RedLine}">
                <Line X1="{Binding X1}" X2="{Binding X2}" Y1="{Binding Y1}" Y2="{Binding Y2}" Stroke="Red" StrokeThickness="4"/>
            </DataTemplate>
        </ItemsControl.Resources>
        <ItemsControl.ItemsSource>
            <CompositeCollection>
                <CollectionContainer Collection="{Binding Source={StaticResource Ellipses}}"/>
                <CollectionContainer Collection="{Binding Source={StaticResource Lines}}"/>
            </CompositeCollection>
        </ItemsControl.ItemsSource>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding CanvasLeft}"/>
                <Setter Property="Canvas.Bottom" Value="{Binding CanvasBottom}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Grid>

视图模型

public class ViewModel
{
    public ObservableCollection<BlackEllipse> Ellipses { get; set; }
    public ObservableCollection<RedLine> Lines { get; set; }

    public ViewModel()
    {
        Ellipses = new ObservableCollection<BlackEllipse>()
        {
            new BlackEllipse() { CanvasLeft = 1000, CanvasBottom = 650 },
            new BlackEllipse() { CanvasLeft = 900, CanvasBottom = 650 }
        };

        Lines = new ObservableCollection<RedLine>()
        {
            new RedLine { X1 = 1013, X2 = 913, Y1 = 768 - 75 - 650, Y2 = 768 - 75 - 650 }
        };
    }
}

public class BlackEllipse : INotifyPropertyChanged
{
    private double canvasLeft;
    private double canvasBottom;

    public double CanvasLeft
    {
        get { return canvasLeft; }
        set
        {
            canvasLeft = value;
            OnPropertyChanged("CanvasLeft");
        }
    }

    public double CanvasBottom
    {
        get { return canvasBottom; }
        set
        {
            canvasBottom = value;
            OnPropertyChanged("CanvasBottom");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]string prop = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
    }
}

public class RedLine
{
    public int X1 { get; set; }
    public int Y1 { get; set; }

    public int X2 { get; set; }
    public int Y2 { get; set; }
}
公共类视图模型
{
公共可观测集合椭圆{get;set;}
公共可观测集合行{get;set;}
公共视图模型()
{
椭圆=新的ObservableCollection()
{
新的BlackEllipse(){CanvasLeft=1000,CanvasBottom=650},
新的BlackEllipse(){CanvasLeft=900,CanvasBottom=650}
};
行=新的ObservableCollection()
{
新红线{X1=1013,X2=913,Y1=768-75-650,Y2=768-75-650}
};
}
}
公共类BlackEllipse:INotifyPropertyChanged
{
私人双拉票;
私人双拉票;
公众双重游说
{
获取{返回画布左;}
设置
{
画布左=值;
OnPropertyChanged(“CanvasLeft”);
}
}
公众双拉票
{
获取{return canvasBottom;}
设置
{
画布底部=值;
OnPropertyChanged(“CanvasBottom”);
}
}
公共事件属性更改事件处理程序属性更改;
public void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]string prop=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(prop));
}
}
公共类红线
{
公共int X1{get;set;}
公共整数Y1{get;set;}
公共int X2{get;set;}
公共int Y2{get;set;}
}

您已经为省略号定义了Canvas.Left、Canvas.Bottom附加属性。如果希望直线随椭圆一起移动,还必须为直线定义它们

public class RedLine
{
    public double CanvasLeft { get; set; }
    public double CanvasBottom { get; set; }
    public int X1 { get; set; }
    public int Y1 { get; set; }

    public int X2 { get; set; }
    public int Y2 { get; set; }
}
初始化

    Lines = new ObservableCollection<RedLine>()
    {
        new RedLine { CanvasLeft = 0, CanvasBottom = 650, X1 = 1013, X2 = 913, Y1 = 768 - 75 - 650, Y2 = 768 - 75 - 650 }
    };
Lines=新的ObservableCollection()
{
新红线{CanvasLeft=0,CanvasBottom=650,X1=1013,X2=913,Y1=768-75-650,Y2=768-75-650}
};
看一看。您可以将其用于线几何体。谢谢,我已经这样做了)我认为还有一种方法可以通过改变x y坐标来移动它。