Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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_Popup - Fatal编程技术网

Wpf 获取继承的背景的值

Wpf 获取继承的背景的值,wpf,popup,Wpf,Popup,我有一个用户控件,其中有一个弹出窗口,偶尔会隐藏控件的其余部分。大概是这样的: <Grid> <Grid Name="myGrid"> <!-- Some stuff --> </Grid> <Popup Width="{Binding ElementName=myGrid, Path=ActualWidth}" PlacementTarget="{Binding ElementName=

我有一个用户控件,其中有一个弹出窗口,偶尔会隐藏控件的其余部分。大概是这样的:

<Grid>
    <Grid Name="myGrid">
    <!-- Some stuff -->
    </Grid>
    <Popup Width="{Binding ElementName=myGrid, Path=ActualWidth}"
           PlacementTarget="{Binding ElementName=myGrid}"
           Placement="Relative">
        <Border Name="popupBorder">
            <Grid>
            <!--- Slightly different stuff --->
            <Grid>
        </Border>
    </Popup>
</Grid>
在所有情况下,边框的背景都是“null”,弹出窗口只是一个大的黑色块。有办法做到这一点吗


(如果你想告诉我有更好的方法来完成我的任务……我有一个很长的列表;我想用“查看更多”按钮显示前几个项目;整个混乱都在一个items控件中,我不想用“查看更多”按钮来调整项目的大小——因此,在弹出窗口中显示长列表。)

在得到一些建议后,我能够实施以下解决方案:

 public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        puBorder.Background = findBackground(this) ?? SystemColors.ControlBrush;
    }

    private Brush findBackground(DependencyObject element)
    {
        var parent = element;
        while (parent != null)
        {
            var p = parent as Control;
            if (p != null && p.Background != null)
                return p.Background;
            parent = VisualTreeHelper.GetParent(parent);
        }
        return null;
    }

我尝试在转换器中执行相同的操作,但转换器在控件添加到可视化树之前执行(无论如何,我假设-它没有可视化父级),因此没有任何用处。

但是
myGrid
没有定义
背景,因此它也将为空。您不必说。然而,与我的弹出窗口不同,在运行时它确实有一个背景。您知道我如何找到该背景的值并将其分配给我的弹出窗口吗?”因为这就是我在这里要做的。
Grid
有背景还是其他东西有背景<代码>网格
默认情况下不会有背景,除非您进行了设置,我在代码中看不到。也许有别的东西可以通过
样式设置
背景。此外,如果您希望
弹出窗口
支持透明度,则需要设置其他透明的内容,否则所有内容都将变为黑色。您可以检查背景,例如,背景是什么?否,
网格
默认情况下没有背景<代码>窗口
已打开。实际上,
窗口中的
边框
有(用Snoop检查)。Snoop显示
网格的默认背景值,在本例中,该值表示null,在视觉上显示为透明<代码>背景
不像
数据上下文
那样被继承,因此
网格
不会继承
窗口的背景
它只是显示出来。在您提到的这个空项目中,设置
Window.Background
,您将看到背景更改,然后设置
网格的背景,您将看到它覆盖了
窗口的背景
public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var brush = this.GetValue(UserControl.BackgroundProperty);
        popupBorder.Background = (Brush)brush;
    }
 public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        puBorder.Background = findBackground(this) ?? SystemColors.ControlBrush;
    }

    private Brush findBackground(DependencyObject element)
    {
        var parent = element;
        while (parent != null)
        {
            var p = parent as Control;
            if (p != null && p.Background != null)
                return p.Background;
            parent = VisualTreeHelper.GetParent(parent);
        }
        return null;
    }