Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何从代码隐藏中的子用户控件替换父控件的内容_Wpf - Fatal编程技术网

Wpf 如何从代码隐藏中的子用户控件替换父控件的内容

Wpf 如何从代码隐藏中的子用户控件替换父控件的内容,wpf,Wpf,我在auditInventoryViews的代码背后写了一些东西:SchedulerView用户控件来替换父contentControl DSViewContentControl的内容 我尝试使用this.content,但它只替换当前用户控件的内容,其他内容仍保留在用户控件中 所以问题是如何在代码隐藏中替换子控件中的父控件的内容 例如,示例代码 <UserControl> <ContentControl x:Name="DSViewContentControl" >

我在auditInventoryViews的代码背后写了一些东西:SchedulerView用户控件来替换父contentControl DSViewContentControl的内容 我尝试使用this.content,但它只替换当前用户控件的内容,其他内容仍保留在用户控件中

所以问题是如何在代码隐藏中替换子控件中的父控件的内容

例如,示例代码

<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获得父内容控制;