コンポーネントのラインナップUserControl設計時サイズ
-
09-06-2019 - |
質問
作成時にUserControlにコンポーネントのラインナップを、までに便利で任意の高さと幅の値をできるよう、ホテルやレンタカーなどの変化は、Visual Studioデザイナー。する活動を行う意欲を制御したいの高さと幅が定義され、その制御の拡大にくい容器にい場所にすることができるのです。たいのですが占同じ機能を外すことなく、高さと幅の値を構築する前に、私か。(またはを使わずにDockPanelの親コンテナ.)
次のコードの問題:
<Window x:Class="ExampleApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:ExampleApplication3"
Title="Example" Height="600" Width="600">
<Grid Background="LightGray">
<loc:UserControl1 />
</Grid>
</Window>
以下の定義 UserControl1
表示と合理的に設計時にもディスプレイについては固定サイズと実行時間:
<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid Background="LightCyan" />
</UserControl>
以下の定義 UserControl1
ディスプレイについては、ドットデザインの時間が拡大への親 Window1
実行時間:
<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="LightCyan" />
</UserControl>
解決
Visual Studioでの追加の幅と高さの属性をUserControl ーが、コードの背後に挿入するこ
public UserControl1()
{
InitializeComponent();
if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
{
this.Width = double.NaN; ;
this.Height = double.NaN; ;
}
}
このかどうかをチェックした場合の制御がデザインモードに切り替わります。ない場合(つまりランタイム)での設定の幅と高さはNaN(Not a number)の値に設定して削除されたり、Width属性-Height属性にー.
で設計時間の短いプリセットの幅と高さを含む場合は、ユーザー制御形)やランタイムでのドックによっては、その親コンテナ.
い役立っています。
他のヒント
のためのブレンドでは、これまでほとんど知られていないトリックで追加これらの属性をusercontrolまたはウィンドウ
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="600"
このデザインの高さと幅は500-600です。しかしこのことのみをブレンド。なVisual Studioデザイナー。
と、Visual Studioのデザイナーの技術はすべてのことです。ではなぜで使っていVisual Studioデザイナー。;)
このリスト 設計-時刻属性のSilverlightデザイナー.彼らは同じコンポーネントのラインナップ。
しているとのことですすべての d:
価値観のデザイナーなど d:DesignHeight
, d:DesignWidth
, d:IsDesignTimeCreatable
, d:CreateList
数。
います。単なるセットの幅と高さの値を"オート"がインスタンスを生成、およびこれをオーバーライド設計-時間の値をとUserControl.
ie: <loc:UserControl1 Width="auto" Height="auto" />
別のオプションの設定を組み合わせの最小幅とMinHeightにサイズを可能にするデザインの時間がかかりますが、こちらも幅と高さの残る"オート".明らかに、これだけ必要がない場合には、UserControlサイズより小さいのmin値を行います。
この相似解のように用いられるものをブレンド、ご記載いた単純な行動をクラスと付属性の幅-高されるだけ適用しなければならなDesinTime
public static class DesignBehavior { private static readonly Type OwnerType = typeof (DesignBehavior); #region Width public static readonly DependencyProperty WidthProperty = DependencyProperty.RegisterAttached( "Width", typeof (double), OwnerType, new FrameworkPropertyMetadata(double.NaN, new PropertyChangedCallback(WidthChangedCallback))); public static double GetWidth(DependencyObject depObj) { return (double)depObj.GetValue(WidthProperty); } public static void SetWidth(DependencyObject depObj, double value) { depObj.SetValue(WidthProperty, value); } private static void WidthChangedCallback(DependencyObject depObj, DependencyPropertyChangedEventArgs e) { if (DesignerProperties.GetIsInDesignMode(depObj)) { depObj.SetValue(FrameworkElement.WidthProperty, e.NewValue); } } #endregion #region Height public static readonly DependencyProperty HeightProperty = DependencyProperty.RegisterAttached( "Height", typeof (double), OwnerType, new FrameworkPropertyMetadata(double.NaN, new PropertyChangedCallback(HeightChangedCallback))); public static double GetHeight(DependencyObject depObj) { return (double)depObj.GetValue(HeightProperty); } public static void SetHeight(DependencyObject depObj, double value) { depObj.SetValue(HeightProperty, value); } private static void HeightChangedCallback(DependencyObject depObj, DependencyPropertyChangedEventArgs e) { if (DesignerProperties.GetIsInDesignMode(depObj)) { depObj.SetValue(FrameworkElement.HeightProperty, e.NewValue); } } #endregion }
そしておUserControlが利用できるようになります。はこれらの特性にー
<UserControl x:Class="ExtendedDataGrid.Views.PersonOverviewView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:tool="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:b="clr-namespace:ExtendedDataGrid.Behaviors" b:DesignBehavior.Width="600" b:DesignBehavior.Height="200"> <Grid> ... </Grid> </UserControl>
使用の最小幅とMinHeightについて調査を進めている。そのように見えますので、実行時にそのサイズをしています。
いるかも確保される場合は追加の制御、コンテナのデザインモードで表示される。
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
if (this.Parent != null)
{
this.Width = double.NaN;
this.Height = double.NaN;
}
}
いかがですか?
の原answererのためにこのソリューション!な方、興味のある方は、こちらではVB:
If LicenseManager.UsageMode <> LicenseUsageMode.Designtime Then
Me.Width = Double.NaN
Me.Height = Double.NaN
End If
示唆するのLicenseManager.UsageMode物件を見たことがありません前もって以下のコードです。
if(!DesignerProperties.GetIsInDesignMode(this))
{
this.Width = double.NaN;
this.Height = double.NaN;
}
esskar,
私を追加したいときは一般的には常にメソッドを呼び出しの拠点をオーバー"は、"方法です。
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
base.OnVisualParentChanged(oldParent);
...
}
大回避策の方が、私が使っているので自在です。