Zac*_*son 54 .net vb.net wpf user-controls dialog
我有一个WPF应用程序有这三种类型的东西......
UserControlZack1位于我的WindowMain上......
<Window x:Class="WindowMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ProjectName"
...
Name="WindowMain">
<Grid>
...
<local:UserControlZack x:Name="UserControlZack1" ... />
...
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
UserControlZack1显示一个WindowModal dailog框...
Partial Public Class UserControlZack
...
Private Sub SomeButton_Click(...)
'instantiate the dialog box and open modally...
Dim box As WindowModal = New WindowModal()
box.Owner = ?????
box.ShowDialog()
'process data entered by user if dialog box is accepted...
If (box.DialogResult.GetValueOrDefault = True) Then
_SomeVar = box.SomeVar
...
End If
End Sub
End Class
如何将box.Owner设置为正确的Window,我正在运行的WindowMain实例?
我无法使用box.Owner = Me.Owner,因为"'所有者'不是'ProjectName.UserControlZack'的成员."
我不能使用box.Owner = Me.Parent,因为它返回一个Grid,而不是Window.
我无法使用box.Owner = WindowMain,因为"'WindowMain'是一种类型,不能用作表达式."
Mar*_*ser 124
尝试使用
.Owner = Window.GetWindow(this)
Run Code Online (Sandbox Code Playgroud)
Nir*_*Nir 41
要获得顶级窗口,您的控件就在,假设有一个:
(Window)PresentationSource.FromVisual(this).RootVisual
Run Code Online (Sandbox Code Playgroud)
要获得主窗口:
Application.Current.MainWindow
Run Code Online (Sandbox Code Playgroud)
小智 8
MyWpfDialog dialog = new MyWpfDialog();
//remember, this is WinForms UserControl and its Handle property is
//actually IntPtr containing Win32 HWND.
new System.Windows.Interop.WindowInteropHelper(dialog).Owner = this.Handle;
dialog.ShowDialog();
Run Code Online (Sandbox Code Playgroud)
更新以尝试和帮助Greg的评论.该命令适用于主窗口菜单,用户控件中的按钮和用户控件中的上下文菜单.
我会用命令来做.所以有一个类Commands.cs类似于:
public static class Commands
{
public static RoutedUICommand TestShowDialogCommand = new RoutedUICommand("Test command", "TestShowDialog", typeof(Commands));
}
Run Code Online (Sandbox Code Playgroud)
在主窗口中注册这些:(你不需要canshow它默认为true)
public Window1()
{
InitializeComponent();
CommandManager.RegisterClassCommandBinding(typeof(System.Windows.Controls.Control),
new CommandBinding(Commands.TestShowDialogCommand, ShowDialogCommand, CanShowDialogCommand));
}
private void ShowDialogCommand(object sender, ExecutedRoutedEventArgs e)
{
var box = new Window();
box.Owner = this;
box.ShowDialog();
}
private void CanShowDialogCommand(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
Run Code Online (Sandbox Code Playgroud)
这是我的主窗口的xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="322">
<Grid>
<StackPanel>
<Menu>
<MenuItem Header="Test">
<MenuItem Header="ShowDialog" Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}"/>
</MenuItem>
</Menu>
<WpfApplication1:BazUserControl />
</StackPanel>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是我的用户控件的xaml(仅限默认代码)
<UserControl x:Class="WpfApplication1.BazUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Height="300" Width="300">
<Grid>
<StackPanel>
<Button Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}" Content="ClickMe" ></Button>
<TextBox>
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="ShowDialog" Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</StackPanel>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
您可以更进一步,并在控制器类中处理该命令,并使其更多一点MVC.
我通过 XAML 一路爬回让它工作......
box.Owner = DirectCast(DirectCast(DirectCast(Me.Parent, Grid).Parent, Grid).Parent, Window)
但这似乎很不优雅。有没有更好的办法?