我有一个名为parent.xaml的父控件。因为我已经放了一些像Grid,Popup等的控件。
<ScrollViewer>
<StackPanel HorizontalAlignment="Center">
<TextBlock HorizontalAlignment="Center">POC For GOTO in WPF Grid</TextBlock>
<TextBlock>-</TextBlock>
<Button x:Name="btn1" Click="btn1_Click">Click To Use GOTO</Button>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<DataGrid x:Name="grid" ItemsSource="{Binding _CommonClass}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Time}" Header="Time" />
<DataGridTextColumn Binding="{Binding Path=ID}" Header="id" />
<DataGridTextColumn Binding="{Binding Path=Program}" Header="Program" />
<DataGridTextColumn Binding="{Binding Path=Duration}" Header="Duration" />
<DataGridTextColumn Binding="{Binding Path=Description}" Header="Description" />
</DataGrid.Columns>
</DataGrid>
</Grid>
<Popup Name="Popup1" Width="240" Height="auto" Placement="Center" PlacementRectangle="50,50,50,100">
<Border BorderBrush="DarkCyan" BorderThickness="3">
<local:GoToUC x:Name="myUserControlInstance"/>
</Border>
</Popup>
</StackPanel>
</ScrollViewer>
我有一个名为child.xaml的用户控件。
<StackPanel Background="AliceBlue" VerticalAlignment="Center" Height="200">
<Grid Margin="20,20,20,20">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBox x:Name="tb_time" Grid.ColumnSpan="2" Grid.Row="0"></TextBox>
<TextBlock Grid.Column="0" Grid.Row="1">-</TextBlock>
<Button x:Name="btn_goto" Grid.Column="0" Grid.Row="2" Click="btn_goto_Click" Width="90">GOTO</Button>
<Button x:Name="btn_cancel" Grid.Column="1" Grid.Row="2" Click="btn_cancel_Click" Width="50">Close</Button>
</Grid>
</StackPanel>
现在从child.xaml
我需要在父窗口的控件上执行一些操作。就像我想从子窗口关闭父弹出窗口。
我使用过这种方法:
Window _parentWindow = Window.GetWindow(this);
但我不确定如何访问_parentwindow
对象的控件。
答案 0 :(得分:7)
我终于找到了答案
Window parentWindow = Window.GetWindow(this);
object obj= parentWindow.FindName("Popup1");
System.Windows.Controls.Primitives.Popup pop = (System.Windows.Controls.Primitives.Popup)obj;
pop.IsOpen = false;
答案 1 :(得分:0)
this.ParentForm.Close() // is a good way to close the parentform from the user control.