如何强制usercontrol宽度等于设计者宽度?

时间:2017-05-31 19:48:42

标签: wpf user-controls

我在WPF中有一个自定义文本框,我想在设计器视图中调整其宽度时更改其宽度。我使用了Binding ActualHeight,但它不起作用。

enter image description here

如图所示,我在设计模式下调整了TextBox的大小,但它仍然处于相同的宽度,没有改变。如何根据调整大小来更改该控件的Width(仅限)属性?

DottedTextBox

<UserControl x:Class="Easy_Message.DottedTextBox"
         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="50" d:DesignWidth="400">
<Viewbox>
<TextBox x:Name="roottxt" Background="White" 
         BorderThickness="0" Width="{Binding ActualWidth,RelativeSource={RelativeSource TemplatedParent}}" Height="20">
    <TextBox.Template>
        <ControlTemplate TargetType="TextBox">
            <Grid>
                <Border x:Name="b" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
                               BorderThickness="1,0,1,0" BorderBrush="#c2c4cb">
                    <Border BorderThickness="0,1,0,0" BorderBrush="#9da0aa">
                        <Border BorderThickness="0,0,0,1" BorderBrush="#d9dae0">
                            <DockPanel>
                                <TextBox x:Name="orgbox" VerticalContentAlignment="Center"
                                     HorizontalAlignment="Left"
                                     BorderThickness="0"
                                     FontSize="{TemplateBinding FontSize}"
                                     Background="White"
                                     Width="{Binding Path=Width,ElementName=b,Converter={StaticResource wconv}}" SelectionBrush="{TemplateBinding SelectionBrush}"
                                     Height="20">
                                </TextBox>
                                <Border x:Name="brdDot" Background="{TemplateBinding Background}" Grid.Column="1" 
                                    Width="16" Height="{TemplateBinding Height}" HorizontalAlignment="Right"
                                    BorderThickness="0">
                                    <Canvas HorizontalAlignment="Right"
                                                    Width="10" Height="6" 
                                                    Margin="0,0,2.5,2.5">
                                        <Rectangle Width="1.5" Height="1.5" Fill="Black">
                                            <Rectangle.RenderTransform>
                                                <TranslateTransform X="0" Y="2"/>
                                            </Rectangle.RenderTransform>
                                        </Rectangle>
                                        <Rectangle Width="1.5" Height="1.5" Fill="Black">
                                            <Rectangle.RenderTransform>
                                                <TranslateTransform X="4" Y="2"/>
                                            </Rectangle.RenderTransform>
                                        </Rectangle>
                                        <Rectangle Width="1.5" Height="1.5" Fill="Black">
                                            <Rectangle.RenderTransform>
                                                <TranslateTransform X="8" Y="2"/>
                                            </Rectangle.RenderTransform>
                                        </Rectangle>
                                    </Canvas>
                                </Border>
                            </DockPanel>
                        </Border>
                    </Border>
                </Border>
            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=brdDot}" Value="True">
                    <Setter TargetName="brdDot" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                                <GradientStop Color="White" Offset="-0.1"/>
                                <GradientStop Color="#ccdfff" Offset="0.6" />
                                <GradientStop Color="White" Offset="1.1" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="brdDot" Property="BorderThickness" Value="1,0,0,0" />
                    <Setter TargetName="brdDot" Property="BorderBrush" Value="#ccc9c9"/>
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>
</Viewbox>

0 个答案:

没有答案