[英]Predefined UserControl or Control Size
我試圖實現具有預定義大小(寬度和高度)的控件或用戶控件。
我有一個定義大小的枚舉:
public enum ControlSizes
{
// Width x Height
ControlSizeA, // 310 x 220
ControlSizeB, // 310 x 450
ControlSizeC // 310 x 680
}
然后在我的控件中,我定義了一個DependencyProperty和一個允許Size規格的回調方法:
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;
}
主要思想是具有預定義的尺寸,並在設計時能夠選擇一種尺寸,並以該可用尺寸放置控件。
問題是寬度和高度沒有正確保存或分配。
有任何想法嗎?
提前致謝。
假設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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.