バインディング要素の幅(Canvasのサブクラスが自動的に伸びる)

3629 ワード

バインディング要素の大きさはWidthとHeightを思い浮かべますが、wpfのWidthとHeightは表示されていない設定が大きすぎるとNaNです.
元素の大きさを動的に求めるにはActual Width、Actual Heightを使います.
<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>