52

WPF で UserControl を作成するとき、Visual Studio デザイナで変更を表示できるように、それに任意の Height 値と Width 値を与えると便利です。ただし、コントロールを実行するときは、Height と Width を未定義にして、コントロールを配置したコンテナに合わせて拡張する必要があります。前に Height と Width の値を削除せずに、これと同じ機能を実現するにはどうすればよいですか?私のコントロールを構築していますか?(または、親コンテナーで 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>
4

9 に答える 9

81

Blend の場合、あまり知られていないトリックは、これらの属性をユーザー コントロールまたはウィンドウに追加することです。

 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 Designer を使用しない理由です。;)

于 2008-09-16T18:36:06.220 に答える
39

Visual Studio で、Width 属性と Height 属性を UserControl XAML に追加しますが、コード ビハインドでこれを挿入します。

public UserControl1()
{
    InitializeComponent();
    if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
    {
        this.Width = double.NaN; ;
        this.Height = double.NaN; ;
    }
}

これにより、コントロールがデザイン モードで実行されているかどうかが確認されます。そうでない場合 (実行時)、幅と高さを NaN (数値ではない) に設定します。これは、XAML で幅と高さの属性を削除した場合に設定する値です。

そのため、設計時に事前設定された幅と高さ (フォームにユーザー コントロールを配置する場合を含む) があり、実行時に親コンテナーに応じてドッキングされます。

それが役立つことを願っています。

于 2008-09-16T18:44:07.230 に答える
9

以下は、Silverlight Designer のデザイン時属性のリストです。WPF デザイナでも同じです。

、、、およびその他のいくつかのd:値など、Designer で使用可能なすべての値が一覧表示されます。d:DesignHeightd:DesignWidthd:IsDesignTimeCreatabled:CreateList

于 2011-04-03T00:26:31.547 に答える
7

私はいつもこれをしています。コントロールをインスタンス化する場所で幅と高さの値を「auto」に設定するだけで、その UserControl の設計時の値が上書きされます。

すなわち:<loc:UserControl1 Width="auto" Height="auto" />

もう 1 つのオプションは、MinWidth と MinHeight の組み合わせを設計時の作業が可能なサイズに設定し、Width と Height は「自動」のままにすることです。明らかに、これは実行時に UserControl のサイズを最小値よりも小さくする必要がない場合にのみ機能します。

于 2008-09-17T01:57:35.177 に答える
2

Blendで使用されているような同様のソリューションを探していました。あなたの言及により、DesinTimeでのみ適用される2つのプロパティ幅と高さを持つ単純な動作クラスを作成しました

パブリック静的クラス DesignBehavior
{
    private static readonly Type OwnerType = typeof (DesignBehavior);

    #region 幅

    public static readonly DependencyProperty WidthProperty =
        DependencyProperty.RegisterAttached(
            "幅"、
            typeof (double)、
            所有者タイプ、
            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, 値);
    }

    private static void WidthChangedCallback(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        if (DesignerProperties.GetIsInDesignMode(depObj)) {
            depObj.SetValue(FrameworkElement.WidthProperty, e.NewValue);
        }
    }

    #endregion

    #region 高さ

    public static readonly DependencyProperty HeightProperty =
        DependencyProperty.RegisterAttached(
            "身長"、
            typeof (double)、
            所有者タイプ、
            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, 値);
    }


    private static void HeightChangedCallback(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        if (DesignerProperties.GetIsInDesignMode(depObj)) {
            depObj.SetValue(FrameworkElement.HeightProperty, e.NewValue);
        }
    }

    #endregion

}

次に、UserControl で、Xaml でこれらのプロパティを設定するだけです

<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">
    <グリッド>
         ...
    </グリッド>
</ユーザー コントロール>
于 2011-05-06T09:18:51.433 に答える
1

コントロールで MinWidth と MinHeight を使用します。そうすれば、デザイナーに表示され、実行時に必要なサイズに調整されます。

于 2012-08-29T01:56:16.243 に答える
0

これまでに見たことのないLicenseManager.UsageModeプロパティの使用を提案する人もいますが、次のコードを使用しました。

if(!DesignerProperties.GetIsInDesignMode(this))
{
    this.Width = double.NaN;
    this.Height = double.NaN;
}

esskar、

「On」メソッドをオーバーライドするときは、通常、常にベースのメソッドを呼び出す必要があることを付け加えておきます。

protected override void OnVisualParentChanged(DependencyObject oldParent)
{
    base.OnVisualParentChanged(oldParent);

    ...
}

ちなみに、すばらしい回避策です。私も今それを使用しています。

于 2009-07-30T17:30:21.047 に答える
0

このソリューションの元の回答者に感謝します! 興味のある方は、ここに VB があります。

If LicenseManager.UsageMode <> LicenseUsageMode.Designtime Then
    Me.Width = Double.NaN
    Me.Height = Double.NaN
End If
于 2009-01-07T19:58:31.470 に答える
0

私も同様に行いますが、私のソリューションでは、デザイン モードでコンテナーにコントロールを追加すると、適切に表示されることが保証されます。

protected override void OnVisualParentChanged(DependencyObject oldParent)
{
    if (this.Parent != null)
    {
       this.Width = double.NaN;
       this.Height = double.NaN;
    }
}

どう思いますか?

于 2008-11-22T23:21:38.227 に答える