• 一个自定义FrameworkElement的例子


    class:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Media;

    namespace WpfApplication25
    {
        
    public class MyTextBlock : FrameworkElement
        {
            TextBlock t;
            
    public MyTextBlock()
            {
                t 
    = new TextBlock() 
                {
                    Text 
    = "TextTextText",
                    Background 
    = Brushes.Blue,
                    Width 
    = 60,
                    Height 
    = 30
                };
                AddLogicalChild(t);
                AddVisualChild(t);
            }

            
    protected override Size MeasureOverride(Size availableSize)
            {
                t.Measure(availableSize);
                
    return base.MeasureOverride(availableSize);
            }

            
    protected override Size ArrangeOverride(Size finalSize)
            {
                t.Arrange(
    new Rect(new Point(5050), t.DesiredSize));
                
    return base.ArrangeOverride(finalSize);
            }

            
    // In order for the visual tree to be enumerated correctly,
            
    // we must override the GetVisualChild and VisualChildrenCount.
            protected override Visual GetVisualChild(int index)
            {
                
    return t;
            }

            
    protected override int VisualChildrenCount
            {
                
    get
                {
                    
    return 1;
                }
            }
        }
    }
    Xaml :
    <Window x:Class="WpfApplication25.Window1"
        xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
        Title
    ="Window1" Height="300" Width="300"
            xmlns:local
    ="clr-namespace:WpfApplication25">
        
    <Grid>
            
    <local:MyTextBlock></local:MyTextBlock>
        
    </Grid>
    </Window>
  • 相关阅读:
    用pelican搭建完美博客
    对比MySQL,什么场景MongoDB更适用
    客官,您的 Flask 全家桶请收好
    虚拟机安装macos 分辨率不正常修改不了,不能全屏如何解决
    mac开启HiDPI
    虚拟机安装MacOS|unlocker解锁出现闪退问题!
    下载com.vmware.fusion.tools.darwin.zip.tar慢
    VMware虚拟机安装黑苹果MacOS Mojave系统详细教程
    Python格式化输出
    ubuntu进入initramfs,系统黑屏
  • 原文地址:https://www.cnblogs.com/bear831204/p/1415719.html
Copyright © 2020-2023  润新知