2014-02-12 104 views
1

我有一個名爲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我需要做的父窗口的controls.Like一些操作我想從子窗口關閉父彈出。 我已經使用這個方法:

Window _parentWindow = Window.GetWindow(this); 

但我不知道如何訪問控制出_parentwindow對象。

+1

後,作爲回答,並將其標記爲接受。這將使相同問題的人更容易找到答案。 – har07

回答

6

我找到了答案終於

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;

0
this.ParentForm.Close() // is a good way to close the parentform from the user control.