gpt4 book ai didi

c# - 如何从用户控件窗口访问父窗口的控件

转载 作者:太空宇宙 更新时间:2023-11-03 18:31:24 24 4
gpt4 key购买 nike

我有一个名为 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 对象访问控件。

最佳答案

终于找到答案了

Window parentWindow = Window.GetWindow(this);<br/>
object obj= parentWindow.FindName("Popup1");
System.Windows.Controls.Primitives.Popup pop = (System.Windows.Controls.Primitives.Popup)obj;<br/>
pop.IsOpen = false;

关于c# - 如何从用户控件窗口访问父窗口的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21719133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com