WPF XAML数据绑定与动态调整UserControl的大小

时间:2017-09-05 12:34:33

标签: c# wpf xaml user-controls

我有一个带有包含文本框的网格的UserControl。此对象使用框中的文本动态调整自身大小。我需要将此对象的尺寸绑定到我的模型。绑定UserControl或Grid的'Width'和'Height'将打破动态调整大小,因为我现在无法处理模型中声明的初始大小。我尝试使用'minHeight'和'minWidth',但这些不会将数据发送回模型,因为最小尺寸永远不会改变。我试图摆弄不同的模式(Oneway,ToWay等......),但没有运气。

总而言之:我需要一种方法来绑定维度,同时使用文本框中的文本保持动态调整大小。

<UserControl x:Class="_02350Demo.View.ClassBoxUserControl"
         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" 
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         Canvas.Left="{Binding X}" Canvas.Top="{Binding Y}"
         Width="{Binding Width}" Height="{Binding Height}">
<UserControl.InputBindings>
    <KeyBinding Modifiers="Control" Key="Z" Command="{Binding UndoCommand}" />
    <KeyBinding Modifiers="Control" Key="Y" Command="{Binding RedoCommand}" />
</UserControl.InputBindings>


<Grid>
    <Rectangle Opacity="{Binding DataContext.ModeOpacity, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" StrokeThickness="6" StrokeDashArray="3.1">
        <!-- The fill (background) color of the ellipse is a radial (center to edge) gradient (more than one color) brush. -->
        <Rectangle.Fill>
            <RadialGradientBrush>
                <GradientStop Color="Black" Offset="0.0" />
            </RadialGradientBrush>
        </Rectangle.Fill>


        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDown">
                <cmd:EventToCommand Command="{Binding DataContext.MouseDownShapeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseMove">
                <cmd:EventToCommand Command="{Binding DataContext.MouseMoveShapeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
            <i:EventTrigger EventName="MouseUp">
                <cmd:EventToCommand Command="{Binding DataContext.MouseUpShapeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Rectangle>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBox Text="{Binding ContentClass}"  HorizontalAlignment="Stretch" AcceptsReturn="True" TextWrapping="Wrap" VerticalAlignment="Stretch" Grid.Row="0" BorderThickness="1,1,1,1" BorderBrush="Gray" TextAlignment="Center" Margin="30,0,30,0"/>
        <TextBox Text="{Binding ContentFields}" HorizontalAlignment="Stretch" AcceptsReturn="True" TextWrapping="Wrap" VerticalAlignment="Stretch" Grid.Row="1" BorderThickness="1,1,1,1" BorderBrush="Gray"/>
        <TextBox Text="{Binding ContentMethods}" HorizontalAlignment="Stretch" AcceptsReturn="True" TextWrapping="Wrap" VerticalAlignment="Stretch" Grid.Row="2" BorderThickness="1,1,1,1" BorderBrush="Gray"/>
    </Grid>

</Grid>

1 个答案:

答案 0 :(得分:0)

如果您只是希望自己的视图模型了解UserControl的动态尺寸,则可以处理SizeChanged的{​​{1}}事件并设置UserControl和视图模型的Width来源属性分别为Height的{​​{1}}和ActualWidth属性:

ActualHeight

如果您想要:https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF

,可以将其包含在行为中