2017-09-05 3 views
0

グリッドにテキストボックスを含むUserControlがあります。このオブジェクトは、ボックス内のテキストを使用して動的にサイズ変更されます。このオブジェクトのディメンションをモデルにバインドする必要があります。私は今モデルの中で宣言されている初期サイズは何かを突きつけているので、UserControlまたはGridの '幅'と '高さ'をバインドすると動的なサイズ変更が破られます。私は '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> 

+0

固定された、ユーザーが設定したサイズのために幅と高さが使用されていますが、代わりにActualWidthとActualHeightを探しているかもしれません。これらは、WPFによって計算されて表示されるコンポーネントの実際のサイズを含みます。 – CShark

答えて

0

あなたは、単にあなたのビューモデルがUserControlの動的なサイズを知りたい場合は、UserControlSizeChangedイベントを処理してまで、あなたのビューモデルのWidthHeightソースプロパティを設定することができますそれぞれUserControlActualWidthActualHeightプロパティ:

private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) 
{ 
    var vm = DataContext as YourViewModel; 
    vm.Height = ActualHeight; 
    vm.Width = ActualWidth; 
} 

あなたはトンを包むことあなたがしたいならば、彼の行動に彼:https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF

関連する問題