컨트롤의 실제 너비와 높이를 기반으로 상당히 비싼 계산을 수행해야하는 경우가 있습니다. 이 컨트롤의 ActualWidth 및 ActualHeight를 내 코드의 속성에 바인딩했습니다. 나는 Actualheight가 작은 단계에서 변화되고 있음을 발견했다. 레이아웃 엔진 때문이라고 생각합니다.스케일링 기하학 실제 높이와 너비에 따라 사용자 정의 컨트롤의 경로가 매우 느립니다.
A는 XAML 버전
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sup="clr-namespace:RowAnimation.Support"
xmlns:vm="clr-namespace:RowAnimation.ViewModels"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:RowAnimation.Views"
x:Name="RowerAnimation"
x:Class="RowAnimation.Views.RowerAnimationControl"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance {x:Type vm:RowerAnimationControlVM}}"
d:DesignHeight="497" d:DesignWidth="741">
<Grid>
<DockPanel >
<Grid Margin="5" x:Name="canvas1" ClipToBounds="True" VerticalAlignment="Top">
<sup:DataPiping.DataPipes>
<sup:DataPipeCollection>
<sup:DataPipe Source="{Binding ActualWidth, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"
Target="{Binding ViewportWidth, Mode=OneWayToSource}"/>
<sup:DataPipe Source="{Binding ActualHeight, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"
Target="{Binding ViewportHeight, Mode=OneWayToSource}"/>
</sup:DataPipeCollection>
</sup:DataPiping.DataPipes>
<Path x:Name="gridLines" Stroke="#FF6083E2" Data="{Binding GridPath, Mode=OneWay}"/>
</Grid>
</DockPanel>
</Grid>
</UserControl>
뷰 모델에서 ViewportHeight 또는 ViewportWidth 속성이 변경 될 때 GridPath이 뷰 모델 내에서 설정된다를 벗었.
내 ViewModel에 중단 점을 설정하면 ViewportWidth가 한 번 변경되지만 ViewportHeight가 작은 증분으로 여러 번 변경됩니다. GridPath를 계산하는 메서드를 주석 처리하면 VieportHeight 중단 점이 한 번만 발생합니다. 그리드 경로의 설정이 새로운 레이아웃 계산을 트리거하는 것처럼 보입니다.
제 실제 구현에서는 그리드가 컨트롤의 맨 아래에 도킹되어 있는데, 이것이 XAML에 Dockpanel이있는 이유입니다.
저는 MVVM을 사용하기 때문에 this Stackoverflow link에서 DataPiping을 사용하여 내 ViewModel에서 ActualHeight 및 ActualWidth를 가져옵니다.
아무도 무슨 일이 일어나고 있는지, 어떻게 피할 수 있는지 생각하지 못합니까? GridPath의 설정 (실제 구현에 PathGeometries가 더 많음)으로 인해 창 크기를 8 초 이상 조정할 수 있습니다. 또한
:
흠, 내가 충분히 기하학을 이해하지 못하는 것 같다. 여기에 내가 그리드를 채우는 데 사용하는 코드는 다음과 같습니다
private void PopulateGrid()
{
PathGeometry path = new PathGeometry();
Rect rect = new Rect(converter.NormalizePoint(xMin, yMin), converter.NormalizePoint(xMax, yMax));
RectangleGeometry border = new RectangleGeometry(rect);
path.AddGeometry(border);
for (double y = 0; y < yMax; y += 30) {
LineGeometry line = new LineGeometry(converter.NormalizePoint(xMin, y), converter.NormalizePoint(xMax, y));
path.AddGeometry(line);
}
GridPath = path;
}
내가 처음 4 줄을 주석 때 다음의 ActualHeight이 설정되어 있지 않고 내 사용자 컨트롤에는 그리드를 볼 수 없습니다. 사각형은 레이아웃 엔진을 작동 시키지만 LineGeometry는 작동하지 않습니다.
xMin, yMin, xMax 및 xMin은 월드 좌표에 있고 NormalizePoint는 현재 ViewportWidth 및 ViewportHeight에 따라 화면 좌표로 변환합니다.
여기서 정확히 무엇을하려하는지 모르겠으므로이 방법은 적용되지 않을 수 있습니다. 그러나 경로의 크기를 컨트롤의 크기로 조절하려면 'ViewBox'안에 넣기 만하면됩니다. 그러면 자동으로 모든 크기 조정 및 다시 그리기가 처리됩니다. –
ViewBox는 선 두께를 포함하여 모든 것을 확장합니다. 나는 그것을 원하지 않는다. 아직도 다른 솔루션을 얻을 수 없다면 그걸 실험 해 볼 수 있습니다. –
흠 ..'Path' 대신에'Geometry'를 사용할 수 있습니다.렌더 변환을'Geometry'에 적용하면 좌표가 스케일됩니다. 라인 스타일은 크기가 조정되지 않은 별도의'Pen'에 의해 지정되기 때문입니다. 하지만 좀 더 복잡해지기 시작합니다. –