下面的代碼可以解決你的問題:
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>
代碼:
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; }
}
@Dheeraj這不是重複。 – Kcvin
窗口的定位是一個僅供查看的相關問題,因此您不應該試圖在您的ViewModel中的屬性中綁定頂部,左側,寬度,高度 - 相反,請在代碼背後執行 – Kcvin
刪除我的評論 – Dheeraj