[英]Using WPF Attached Properties to layout an LOB form
我已經在應用程序中編寫了很多數據輸入類型的表格,得出的結論是我需要使其變得更簡單。 閱讀一些內容后,似乎可以使用子類ItemsControl代表表單來完成。
我已經做到了,現在有類似
<MySubClassedForm></MySubClassedForm>
我現在想做的是設置一個附加屬性“ LabelText”,以便可以在內部的任何控件上使用它。
舉個例子,
<MySubClassedForm>
<TextBox MySubClassedForm.LabelText="Surname" />
<Image MySubClassedForm.LabelText="LabelText" />
</MySubClassedForm>
附加屬性定義:-
public static DependencyProperty LabelTextProperty = DependencyProperty.RegisterAttached("LabelText", typeof(string), typeof(MySubclassedForm),
new UIPropertyMetadata(string.Empty));
public string LabelText
{
get { return (string)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
我首先將附加屬性放在MySubClassedForm上,然后收到以下錯誤:
附加屬性'MySubClassedForm.LabelText'未在'TextBox'或其基類之一上定義。
請您告訴我我做錯了什么以及我需要做些什么才能完成這項工作?
謝謝亞歷克斯
您將需要定義靜態getter和setter方法:
public static readonly DependencyProperty LabelTextProperty =
DependencyProperty.RegisterAttached(
"LabelText", typeof(string), typeof(MySubclassedForm),
new UIPropertyMetadata(string.Empty));
public static string GetLabelText(DependencyObject obj)
{
return (string)obj.GetValue(LabelTextProperty);
}
public static void SetLabelText(DependencyObject obj, string value)
{
obj.SetValue(LabelTextProperty, value);
}
在此處獲取有關“ 自定義附加屬性 ”的更多信息。
您應該看看magellan它既具有WPF表單引擎又具有出色的MVC框架。 任何一個都可以不使用另一個。
它可以讓你做
<Form>
<Field For="{Binding Path=Server.Server}" />
<Field For="{Binding Path=Server.CachedExchangeMode}" />
<Field For="{Binding Path=Server.Username}" />
<Field For="{Binding Path=Server.SecurityMode}" />
</Form>
它將為視圖模型上的屬性自動生成正確的輸入字段類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.