는 아래의 코드는 문제를 해결 :
XAML :
<Grid>
<Border x:Name="Brd" Margin="50,46,0,0" BorderBrush="#FFB82E2E" Background="#FFC7DC42" BorderThickness="5" CornerRadius="5">
<Label Content="a label"/>
</Border>
<Button Content="Button" HorizontalAlignment="Left" Margin="32,15,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
코드 :
private void Button_Click(object sender, RoutedEventArgs e)
{
Window w = new Window();
w.Width = Brd.ActualWidth;
w.Height = Brd.ActualHeight;
Point pt = Brd.PointToScreen(new Point(0, 0));
PresentationSource source = PresentationSource.FromVisual(this);
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(pt);
w.Top = targetPoints.Y;
w.Left = targetPoints.X;
w.Show();
}
수정 # 1, 순수 MVVM를 들어
(사용자의 요청 후) 당신 첨부 된 동작을 작성해야합니다. 모든 것을 MainWindow
네임 스페이스 아래에 두었습니다.
사용법 :
<Window.Resources>
<Window x:Key="NewWindowKey"/>
</Window.Resources>
<Grid>
<Border x:Name="Brd" Margin="50,46,0,0" BorderBrush="#FFB82E2E" Background="#FFC7DC42" BorderThickness="5" CornerRadius="5">
<Label Content="a label"/>
</Border>
<Button Content="Button" HorizontalAlignment="Left" Margin="32,15,0,0" VerticalAlignment="Top" Width="75">
<local:ApplicationBehaviors.WindowPlacementBehavior>
<local:NewWindowToShowParameters NewWindow="{StaticResource NewWindowKey}" TargetVisualName="Brd"/>
</local:ApplicationBehaviors.WindowPlacementBehavior>
</Button>
</Grid>
코드 :이 @Dheeraj
public static class ApplicationBehaviors
{
public static NewWindowToShowParameters GetWindowPlacementBehavior(DependencyObject obj)
{
return (NewWindowToShowParameters)obj.GetValue(WindowPlacementBehaviorProperty);
}
public static void SetWindowPlacementBehavior(DependencyObject obj, NewWindowToShowParameters value)
{
obj.SetValue(WindowPlacementBehaviorProperty, value);
}
// Using a DependencyProperty as the backing store for WindowPlacement. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WindowPlacementBehaviorProperty =
DependencyProperty.RegisterAttached("WindowPlacementBehavior", typeof(NewWindowToShowParameters), typeof(ApplicationBehaviors), new PropertyMetadata(null, new PropertyChangedCallback(WindowPlacementChanged)));
private static void WindowPlacementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Button ctrl = d as Button;
ctrl.Click += ((s, args) =>
{
DependencyObject parent = VisualTreeHelper.GetParent(ctrl);
while ((parent as Window) == null)
parent = VisualTreeHelper.GetParent(parent);
Window rootWindow = (Window)parent;
NewWindowToShowParameters newWindowParams = ((NewWindowToShowParameters)e.NewValue);
Window newWin = newWindowParams.NewWindow;
Border b = (Border) rootWindow.FindName(newWindowParams.TargetVisualName);
newWin.Width = b.ActualWidth;
newWin.Height = b.ActualHeight;
Point pt = b.PointToScreen(new Point(0, 0));
PresentationSource source = PresentationSource.FromVisual(rootWindow);
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(pt);
newWin.Top = targetPoints.Y;
newWin.Left = targetPoints.X;
newWin.Show();
});
}
}
public class NewWindowToShowParameters
{
public Window NewWindow { get; set; }
public string TargetVisualName { get; set; }
}
는 중복되지 않습니다. – Kcvin
창 위치 지정은보기 전용 문제이므로 ViewModel에서 속성의 Top, Left, Width, Height를 바인딩하지 말아야합니다. 대신 코드 숨김으로 처리하십시오. – Kcvin
내 의견 – Dheeraj