Wpf 如何从代码隐藏中的子用户控件替换父控件的内容
我在auditInventoryViews的代码背后写了一些东西:SchedulerView用户控件来替换父contentControl DSViewContentControl的内容 我尝试使用this.content,但它只替换当前用户控件的内容,其他内容仍保留在用户控件中 所以问题是如何在代码隐藏中替换子控件中的父控件的内容 例如,示例代码Wpf 如何从代码隐藏中的子用户控件替换父控件的内容,wpf,Wpf,我在auditInventoryViews的代码背后写了一些东西:SchedulerView用户控件来替换父contentControl DSViewContentControl的内容 我尝试使用this.content,但它只替换当前用户控件的内容,其他内容仍保留在用户控件中 所以问题是如何在代码隐藏中替换子控件中的父控件的内容 例如,示例代码 <UserControl> <ContentControl x:Name="DSViewContentControl" >
<UserControl>
<ContentControl x:Name="DSViewContentControl" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" ></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!-- region scheduler -->
<StackPanel Grid.Row="1" Grid.Column="1" Margin="40,5,0,0">
some content
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="1" Margin="40,5,0,0">
<auditInventoryViews:SchedulerView/>
</StackPanel>
</Grid>
</ContentControl>
</UserControl>
可以通过父属性从子控件引用父控件。例如,要从SchedulerView代码隐藏获取DSViewContentControl,请执行以下操作:
StackPanel parentStackpnl = (StackPanel)this.Parent;
Grid grandParentGrid = (Grid)parentStackpnl.Parent;
ContentControl DSViewContentControl = (ContentControl)grandParentGrid.Parent;
//at this point doing operation on DSViewContentControl is straightforward
谢谢您尝试了您的方法,并且能够使用ContentControl DSViewContentControl=ContentControlGridStackPanelthis.parent.parent.parent获得父内容控制;