2013-10-22 29 views
0

我試圖實現具有預定義大小(寬度和高度)的控件或用戶控件。預定義的用戶控件或控件大小

我有一個定義大小的枚舉:

public enum ControlSizes 
{ 
    // Width x Height 
    ControlSizeA, // 310 x 220 
    ControlSizeB, // 310 x 450 
    ControlSizeC // 310 x 680 
} 

然後在我的控制我已經定義了一個DependencyProperty和一個回調方法,以實現尺寸規格:

public static readonly DependencyProperty ControlSizeProperty = DependencyProperty.Register 
     ("ControlSize", 
     typeof(ControlSizes), 
     typeof(CustomControl), 
     new PropertyMetadata(ControlSizes.ControlSizeA, OnControlSizePropertyChanged)); 

public ControlSizes ControlSize 
{ 
    get { return (ControlSize)GetValue(ControlSizeProperty); } 
    set { SetValue(ControlSizeProperty, value); } 
} 

private static void OnControlSizePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) 
{ 
    CustomControl customControl = source as CustomControl; 
    Size controlSize = ControlSizeConverter.ConvertToSize(customControl.ControlSize); 
    customControl.Width = controlSize.Width; 
    customControl.Height = controlSize.Height; 
} 

主要的想法是有預定義的尺寸和設計時間可以選擇一種尺寸,並按照可用尺寸放置控件。

問題是寬度和高度未被正確保存或分配。

任何想法?

在此先感謝。

+0

是什麼'customControl。 ViewSize'? –

+0

對不起,這是一個錯字。它的大小controlSize = ControlSizeConverter.ConvertToSize(customControl.ControlSize); –

回答

0

假設ControlSizeConverter.ConvertToSize得到一個ControlSizes對象,並返回一個Size,並且希望CustomControl來調整它的大小,當ControlSize DP的變化,我覺得你的回調應該是這樣的:

private static void OnControlSizePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) 
{ 
    CustomControl customControl = source as CustomControl; 
    Size controlSize = ControlSizeConverter.ConvertToSize(ControlSize); 
    customControl.Width = controlSize.Width; 
    customControl.Height = controlSize.Height; 
} 
+0

謝謝。這正是我的方式。問題是你設置了一個ControlSize,但是屬性Width和Height沒有改變。控件的大小看起來是大小的,但屬性並不是。如果您關閉並重新打開設計器,則不會應用更改。 –