2016-07-12 8 views
0

私は、UserControlsの一般化されたコンテナとして機能する基本ウィンドウを持っています。幅と高さが子アイテムのサイズによって決定されないように見えることを除いて、期待通りに機能します(幅と高さがautoに設定されていると、私は予想しています)。次のようにベースウィンドウのXAMLは、次のとおりです。ItemsControlにウィンドウの幅と高さを自動的に設定します。

<local:BaseView x:Class="Program.UI.Views.BaseWindowView" 
      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:local="clr-namespace:Program.UI.Views" 
      xmlns:converters="clr-namespace:Program.UI.Converters" 
      xmlns:presenter="clr-namespace:Program.Presenter" 
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
      mc:Ignorable="d" 
      ResizeMode="NoResize" WindowStyle="None" 
      d:DesignHeight="300" d:DesignWidth="300" AllowsTransparency="True"> 
<i:Interaction.Triggers> 
    <i:EventTrigger EventName="Closing"> 
     <presenter:EventToCommand Command="{Binding Mode=OneWay, Path=CloseWindow}" PassEventArgsToCommand="True"/> 
    </i:EventTrigger> 
</i:Interaction.Triggers>  
<local:BaseView.Resources> 
    <ResourceDictionary> 
     <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> 
     <converters:SystemEventToForegroundColor x:Key="SystemEventToForegroundColor" /> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Program;component/UI/Templates/Generic.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</local:BaseView.Resources> 
<local:BaseView.Foreground> 
    <SolidColorBrush Color="Black" Opacity="100"/> 
</local:BaseView.Foreground> 
<local:BaseView.Background> 
    <SolidColorBrush Color="White" Opacity="0"/> 
</local:BaseView.Background> 
<Border BorderBrush="#FF838383" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="1" Height="auto" Width="auto" Margin="0,0,5,5"> 
    <Canvas Background="#E8F6F6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="auto" Width="auto"> 
     <Canvas.Effect> 
      <DropShadowEffect RenderingBias="Quality" Opacity="0.3" ShadowDepth="3" BlurRadius="4"/> 
     </Canvas.Effect> 
     <DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
        Margin="0,0,0,0" x:Name="ReferenceInfo" Canvas.Left="0" Canvas.Top="0" 
        Width="{Binding ActualWidth, ElementName=InfoCanvas}" 
        Height="{Binding ActualHeight, ElementName=InfoCanvas}" 
        d:DesignWidth="294" 
        d:DesignHeight="294"> 

      <Grid Width="auto" Height="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
       </Grid.RowDefinitions> 
       <Grid Grid.Row="0" Width="auto" HorizontalAlignment="Stretch" Height="30" VerticalAlignment="Top"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition /> 
         <ColumnDefinition Width="30" /> 
        </Grid.ColumnDefinitions> 
        <Border Grid.Column="0" BorderBrush="#FF838383" BorderThickness="0,0,0,1" Height="30" VerticalAlignment="Top"> 
         <Label Style="{StaticResource WindowHeaderControlTitle}" MouseDown="Label_MouseDown"/> 
        </Border> 
        <Border Grid.Column="1" BorderBrush="#FF838383" BorderThickness="1,0,0,1"> 
         <Label Style="{StaticResource WindowHeaderControlCloseLabel}" MouseEnter="Label_MouseEnter" MouseLeave="Label_MouseLeave" MouseLeftButtonUp="Label_MouseLeftButtonUp" MouseLeftButtonDown="Label_MouseLeftButtonDown" FontSize="14" FontFamily="Segoe UI Black" FontStretch="UltraExpanded" Content="X"/> 
        </Border> 
       </Grid> 
       <ItemsControl Grid.Row="1" Height="auto" Width="auto" ItemsSource="{Binding ChildView}"/> 
      </Grid> 

     </DockPanel> 
    </Canvas> 
</Border> 

ビューがあるため、Excelのリボンボタンのクリックイベント、コードビハインドで生成されます。

var childView = new DatapointDefinitionsView(); 
childView.DataContext = new DatapointDefinitionsViewModel(); 

ApplicationData.Presenter.ShowView<BaseWindowView>(
    new BaseWindowViewModel(childView, ApplicationData.Presenter), true); 

ShowViewコードは次のとおりです。

private BaseWindowView _windowView; 
    public void ShowView<T>(BaseWindowViewModel viewModel, bool asModal) where T : BaseWindowView, new() 
    { 
     _windowView = new T 
     { 

      DataContext = viewModel, 
      ShowInTaskbar = false, 
      Title = viewModel.Caption, 

     }; 
     //Width = viewModel.ChildView[0].Width, 
     //Height = viewModel.ChildView[0].Height 
     if (asModal) 
     { 
      _windowView.ShowDialog(); 
      _windowView = null; 
     } 
     else 
     { 
      _windowView.Show(); 
     } 
    } 

子の高さに明示的に幅と高さを設定すると、幅は指定された幅になりますが、高さには影響しません。ただし、値が固定されており、ユーザーコントロールのサイズが変更された場合は更新されないため、これは満足のいく解決策ではありません。

私が欲しいものを達成する別の方法はありますか?

+2

ウィンドウで 'SizeToContent =" WidthAndHeight "'を試しましたか? –

+0

'ResizeMode =" NoResize "'を設定すると、ウィンドウのサイズをどのように変更するのですか? – lokusking

+0

@lokusking 'ResizeMode =" NoResize "'はテスト目的のために設定されました。私は仕事中に何もせずに初期サイズを設定しようとしていました。通常は "CanResize"に設定され、1600x900(測定されていない)のデフォルトサイズのウィンドウを表示します – Sufo

答えて

0

次のようにだから私は、すべての必要な機能を、最後にこれを解決することができた:

私はDockPanel Height="{Binding ElementName=ReferenceInfo, Path=ActualHeight}"

の高さに(XAMLで)キャンバスの高さを結合したがItemsControlの要素 Width="{Binding ElementName=ChildUserControl, Path=ActualWidth}"

の幅は私が以前の提案を@lokuskingごとに高さと幅DockPanelから、再度有効にSizeToContent="WidthAndHeight"ウィンドウの属性を削除しました。

private void BaseView_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     var window = sender as Window; 

     if (ChildUserControl.HasItems) 
     { 
      var chlidControl = ChildUserControl.Items[0] as UserControl; 
      var context = (BaseWindowViewModel)window.DataContext; 

      // Ignore the first 3 resize events - these occur on view generation 
      if (!context.InitialResizeComplete) 
       if (context.ResizeOperations < 3) 
       { 
        context.ResizeOperations++; 
        return; 
       } 

      context.InitialResizeComplete = true; 

      // Subtract all fixed size elements from window dimensions 
      chlidControl.Width = window.ActualWidth - (OuterBorder.BorderThickness.Left + OuterBorder.BorderThickness.Right + OuterBorder.Margin.Right + OuterBorder.Margin.Left); 
      chlidControl.Height = window.ActualHeight - (OuterBorder.BorderThickness.Top + OuterBorder.BorderThickness.Bottom + OuterBorder.Margin.Top + OuterBorder.Margin.Bottom + TaskBarGrid.Height); 
     } 
    } 

私が何を決定するために、まだきた:私は窓SizeChangedイベントにフックし、次のコードを追加(動的に応じてサイズを変更するユーザーコントロール子の内容をと持っている)CanResizeWithGrip機能を有効にするために

最初の3つのサイズ変更操作を駆動します。私の最初の推測は初期化、次にx、そしてy - でしたが、反射ではベースビューの場合は1、ベースウィンドウの場合は1、ユーザコントロールの場合は1になる可能性があります。その場合、複数のビューを持つ添付されたユーザーコントロールがこのアプローチを使用すると問題になることがありますが、まだテストしていません。

誰かがこの機能を見つけることができたら助けてくれた人に感謝します。

関連する問題