【问题标题】:how to replace the content of the parent control from child user control in code behind如何在后面的代码中从子用户控件替换父控件的内容
【发布时间】:2014-10-19 19:44:35
【问题描述】:

我在 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>

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您可以通过Parent 属性从子级引用父控件。例如从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 = (ContentControl)(((Grid)((StackPanel)this.Parent).Parent).Parent);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多