绑定元素的长宽(Canvas的子类自动伸展)

绑定元素的大小一般会想到Width和Height但是wpf里的Width和Height如果没有显示的设置过大小那么得到的就是NaN

要动态得到元素的大小要用ActualWidth、ActualHeight

<UserControl x:Class="园区展示程序.MyRichTextBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Name="root">
    <Canvas>
        <RichTextBox Height="{Binding ElementName=root,  Path=ActualHeight}" Width="{Binding ElementName=root,  Path=ActualWidth}" Name="content" IsReadOnly="True" Foreground="White" BorderBrush="{x:Null}" SelectionOpacity="0" BorderThickness="0"  Background="{x:Null}">
        </RichTextBox>
        <Label Height="{Binding ElementName=root,  Path=ActualHeight}" Width="{Binding ElementName=root,  Path=ActualWidth}" Opacity="0" />
    </Canvas>
</UserControl>
原文地址:https://www.cnblogs.com/wangjixianyun/p/2982660.html