Tôi có một điều khiển người dùng mà tôi tải vào MainWindow
lúc chạy. Tôi không thể có một tay cầm trên cửa sổ chứa từ UserControl
.
Tôi đã thử this.Parent
, nhưng nó luôn luôn là null. Có ai biết làm thế nào để có một điều khiển đến cửa sổ chứa từ một điều khiển người dùng trong WPF không?
Đây là cách điều khiển được tải:
private void XMLLogViewer_MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem application = sender as MenuItem;
string parameter = application.CommandParameter as string;
string controlName = parameter;
if (uxPanel.Children.Count == 0)
{
System.Runtime.Remoting.ObjectHandle instance = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, controlName);
UserControl control = instance.Unwrap() as UserControl;
this.LoadControl(control);
}
}
private void LoadControl(UserControl control)
{
if (uxPanel.Children.Count > 0)
{
foreach (UIElement ctrl in uxPanel.Children)
{
if (ctrl.GetType() != control.GetType())
{
this.SetControl(control);
}
}
}
else
{
this.SetControl(control);
}
}
private void SetControl(UserControl control)
{
control.Width = uxPanel.Width;
control.Height = uxPanel.Height;
uxPanel.Children.Add(control);
}