2011-10-26 20 views
0

入力ボックスの私のearlier exampleに、私はウィンドウ変数を持っています。私はいくつかのコントロール(テキストボックス、ラベル、ボタン)を作成しました。これらのコントロールの親はキャンバスです。キャンバスの親はViewBoxです(ViewBoxには子が1つしか含まれないため)ViewBoxの親がウィンドウです。 階層はWindow->Viewbox->Canvas-> Controlsのようです。これらのコントロールの作成と子育てはすべて動的に行われます。ダイナミックビューボックスが伸びていない

 winInputDialog = new Window(); 
     lblPrompt = new Label(); 
     btnOK = new Button(); 
     btnCancel = new Button(); 
     txtInput = new TextBox(); 
     cvContainer = new Canvas(); 
     VB = new Viewbox(); 

     // 
     // lblPrompt 
     // 
     lblPrompt.Background = new SolidColorBrush(SystemColors.ControlColor); 
     lblPrompt.FontFamily = new FontFamily("Microsoft Sans Serif"); 
     lblPrompt.FontSize = 12; 
     lblPrompt.Background = new SolidColorBrush(Colors.Transparent); 
     lblPrompt.FontStyle = FontStyles.Normal; 
     lblPrompt.Margin = new Thickness(8, 9, 0, 0); 
     lblPrompt.Name = "lblPrompt"; 
     lblPrompt.Width = 302; 
     lblPrompt.Height = 82; 
     lblPrompt.TabIndex = 3; 
     // 
     // btnOK 
     // 
     btnOK.Margin = new Thickness(322, 8, 0, 0); 
     btnOK.Name = "btnOK"; 
     btnOK.Width = 64; 
     btnOK.Height = 24; 
     btnOK.TabIndex = 1; 
     btnOK.Content = "OK"; 
     btnOK.Click += new RoutedEventHandler(btnOK_Click); 
     // 
     // btnCancel 
     //   
     btnCancel.Margin = new Thickness(322, 40, 0, 0); 
     btnCancel.Name = "btnCancel"; 
     btnCancel.Width = 64; 
     btnCancel.Height = 24; 
     btnCancel.TabIndex = 2; 
     btnCancel.Content = "Cancel"; 
     btnCancel.Click += new RoutedEventHandler(btnCancel_Click); 
     // 
     // txtInput 
     // 
     txtInput.Margin = new Thickness(8, 70, 0, 0); 
     txtInput.Name = "txtInput"; 
     txtInput.Width = 379; 
     txtInput.Height = 25; 
     txtInput.TabIndex = 0; 
     // 
     //Canvas 
     // 

     double width = System.Windows.SystemParameters.PrimaryScreenWidth/3, height = System.Windows.SystemParameters.PrimaryScreenHeight/4; 

     cvContainer.Height = height; 
     cvContainer.Width = width; 

     cvContainer.Children.Add(txtInput); 
     cvContainer.Children.Add(btnCancel); 
     cvContainer.Children.Add(btnOK); 
     cvContainer.Children.Add(lblPrompt); 
     cvContainer.ClipToBounds = true; 
     // 
     //ViewBox 
     //    
     VB.Stretch = Stretch.Fill; 
     VB.Child = cvContainer; 
     // 
     // InputBoxDialog 
     // 
     winInputDialog.Width = width; 
     winInputDialog.Height = height;    
     winInputDialog.Content = VB; 
     winInputDialog.Icon = new System.Windows.Media.Imaging.BitmapImage(new System.Uri(System.IO.Directory.GetCurrentDirectory() + @"\drop-box-icon.png")); 
     winInputDialog.WindowStartupLocation = WindowStartupLocation.CenterScreen; 
     //winInputDialog.WindowStyle = WindowStyle.SingleBorderWindow; 
     winInputDialog.ResizeMode = ResizeMode.CanResizeWithGrip; 
     winInputDialog.Name = "InputBoxDialog"; 

キャンバスの幅と高さのプロパティをwindowと同じに設定しました。しかし、なぜ私の画面は次のようになります。

screen

はなぜそこには、彼らがビューボックスにいるにもかかわらず、コントロールとウィンドウ枠の間のスペースです。私もCliptoboundsを試みたが、それでも同じだった。

Viewboxの高さと幅を設定すると、Viewboxとは異なり、伸びず動作しません。

この画面を動的に設定する必要があります。どうやって?

サンプルプロジェクトはhttp://122.160.24.172/download/customer_data/InputBox_New.rarです。

答えて

2

ウィンドウをダイナミックレイアウトにしたい場合、なぜ静的なCanvasのようなダイナミックコンテナを使用しないのですか?

あなたはこのようGridを使用することができます -

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="0.8*"/> 
     <ColumnDefinition Width ="0.2*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <TextBlock Text="Hello"/> 
    <Button Grid.Column="1" Content="Ok"/> 
    <Button Grid.Row="1" Grid.Column="1" Content="Cancel"/> 
    <TextBox Grid.Row="2" Grid.ColumnSpan="2" Text="Hello"/> 
</Grid> 

そのサイズが変更された場合は、この方法では、あなたの窓は、それ自体をレイアウトします。 必要に応じて、ボタンのサイズと余白を調整することができます。

正確なピクセル調整の配置レイアウトのサポートが本当に必要でない限り、Canvasを使用しないでください。

また、XAMLではなく、プログラムでウィンドウをレイアウトするのはなぜですか?

+0

実際には、入力ボックスとそれは非常に頻繁にユーザー入力を取るために私のアプリで使用されます。そのXAMLウィンドウの場合は、毎回そのインスタンスを作成して使用する必要があります。これは私のコードが乱雑になります。 Infactは、この動的ウィンドウのStatic関数を使用し、コンポーネント自身を初期化します。 –

関連する問題