我有一個帶有包含文本框的網格的UserControl。該對象通過框中的文本動態調整自身大小。我需要將這個對象的尺寸綁定到我的模型上。綁定UserControl或Grid的'Width'和'Height'會破壞動態調整大小,因爲我現在堅持使用模型中聲明的任何初始大小。我嘗試使用'minHeight'和'minWidth',但這些不會將數據發送回模型,因爲最小尺寸永遠不會改變。我試圖擺弄不同的模式(Oneway,ToWay等),但沒有運氣。動態調整大小的WPF XAML數據綁定UserControl
所以總結一下:我需要一種方法來綁定尺寸,同時保持文本框中文本的動態調整大小。
<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>
雖然寬度和高度用於固定的,用戶設置的大小,你可能會尋找ActualWidth和ActualHeight代替。這些包含由WPF計算和顯示的組件的實際大小。 – CShark