wpf添加新控件原本的控件消失

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf添加新控件原本的控件消失相关的知识,希望对你有一定的参考价值。

动态添加控件——在后台实例化需要添加的控件,然后将这个控件放进容器里
例如Grid.children.add(控件)
原本的控件消失——有两种做法 1.将控件的visibility 设为collapse 2.将容器里这个控件移除
例如Grid.children.remove(控件)
以上这两个逻辑操作放在一个事件里完成就行啦。
参考技术A 用户控件公开的事件,让你的身体回调方法。为了实现 参考技术B 同样的控件,是不是覆盖了!

WPF C# 图像控件在 540 像素以下被剪切/消失

【中文标题】WPF C# 图像控件在 540 像素以下被剪切/消失【英文标题】:WPF C# Image control cutted/disapeared below 540 pixels 【发布时间】:2021-04-01 12:22:56 【问题描述】:

由于某种原因,图像控件不显示低于 540 像素

如果图像控件上边距低于窗口的 540 像素,它就会消失 如果低于则低于 540 像素

С找不到任何解决方案

2 images of clock cut

C# 代码

        TimeZoneInfo newyorkZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        double time44 = Convert.ToDouble(TimeZoneInfo.ConvertTime(DateTime.Now, newyorkZone).ToString("HH"));

        if (time44 >= 12) time44 = time44 - 12;
     
        double time444 = Convert.ToDouble(TimeZoneInfo.ConvertTime(DateTime.Now, newyorkZone).ToString("mm"));
        
        double time44rad = (time44 * 360 / 12) * Math.PI / 180 - Math.PI / 2;
        double time444rad = (time444 * 360 / 60) * Math.PI / 180 - Math.PI / 2;
     

        double xtime444 = 125 + 125 * Math.Cos(time444rad);
        double ytime444 = 125 + 125 * Math.Sin(time444rad);
       
        xtime44 = Math.Round(xtime44, 0);
        int xitime44 = Convert.ToInt32(xtime44);
        
        ytime44 = Math.Round(ytime44, 0);
        int yitime44 = Convert.ToInt32(ytime44);
       
        xtime444 = Math.Round(xtime444, 0);
        int xitime444 = Convert.ToInt32(xtime444);
       
        ytime444 = Math.Round(ytime444, 0);
        int yitime444 = Convert.ToInt32(ytime444);

        int xi = 125;
        int yi = 125;

        GeometryGroup clock4 = new GeometryGroup();
  
        clock4.Children.Add(new EllipseGeometry(new Point(xi, yi), 125, 125));
       
        clock4.Children.Add(new LineGeometry(new Point(xi, yi), new Point(xitime44, yitime44)));
        clock4.Children.Add(new LineGeometry(new Point(xi, yi), new Point(xitime444, yitime444)));
        Pen pen = new Pen(Brushes.Red, 3);
  
        GeometryDrawing aGeometryDrawing4 = new GeometryDrawing();
        aGeometryDrawing4.Geometry = clock4;
        aGeometryDrawing4.Pen = pen;
     
        DrawingGroup aDrawingGroup4 = new DrawingGroup();
      
        aDrawingGroup4.Children.Add(aGeometryDrawing4);
      
        DrawingImage geometryImage4 = new DrawingImage(aDrawingGroup4);

        geometryImage4.Freeze();
  
        cap4time.Source = geometryImage4;

Xaml

        <Image x:Name="cap1time" Margin="100,50" Width="250" Height="250" HorizontalAlignment="Left" 
    VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />
        <Image x:Name="cap3time" Margin="100,500" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />
        <Image x:Name="cap4time" Margin="600,500" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />
        <Image x:Name="cap2time" Margin="600,50" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />

【问题讨论】:

使用其边距定位 WPF 控件绝不是一个好主意 - 将每个图像放置在 Grid 控件的不同行/列中。 这意味着我需要添加网格并将其拆分,然后在单独的行/列中添加图像控件而不使用 Marging? 是的,例如wpf-tutorial.com/panels/grid-rows-and-columns 谢谢你,它有帮助!!! 【参考方案1】:

感谢 Peregrine,这对我有帮助

<Grid Grid.ColumnSpan="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="500"/>
                <ColumnDefinition Width="1320"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="500"/>
                <RowDefinition Height="530"/>
            </Grid.RowDefinitions>
            <Image x:Name="cap1time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="1" Grid.Row="1" />
            <Image x:Name="cap2time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="2" Grid.Row="1" />
            <Image x:Name="cap3time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="1" Grid.Row="2"/>
            <Image x:Name="cap4time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="2" Grid.Row="2" />
            
        </Grid>

【讨论】:

以上是关于wpf添加新控件原本的控件消失的主要内容,如果未能解决你的问题,请参考以下文章

WPF 事件来捕获新添加的控件?

WPF 在控件中添加自定义属性

WPF 将窗口控件封装到类库中使用

WPF:Designer无法添加第三方控件。这导致设计人员重新启动

wpf 动态添加控件和使用

wpf 如何动态向tabitem添加控件