[英]How do I expose custom control properties inside a user control?
我有一個名為TextBoxWithLabelAndUnits
。 從名稱中可以看出,這是一個TextBox
,其前面有一個Label
其后有一個Label
,因此自定義控件如下所示:
-----------------
Label: | | units
-----------------
我公開了幾個依賴項屬性來配置控件,例如:
LabelWidth
LabelText
UnitText
TextBoxWidth
現在,我有一個名為LatLong
的用戶控件,可用於緯度/經度輸入。 XAML看起來像這樣:
<UserControl ...>
<StackPanel>
<TextBoxWithLabelAndUnits LabelText="Latitude:"
UnitText="degrees"
/>
<TextBoxWithLabelAndUnits LabelText="Longitude:"
UnitText="degrees"
/>
</StackPanel>
</UserControl>
它將創建一個用戶控件,如下所示:
-----------------
Latitude: | | degrees
-----------------
-----------------
Longitude: | | degrees
-----------------
現在,我想在項目中使用我的用戶控件。 但是,我希望用戶控件公開屬性,以便在不喜歡默認設置的情況下更改標簽。 我可以將每個對象公開為新的依賴項屬性,如下所示:
LatitudeLabelWidth
LatitudeLabelText
LatitudeUnitText
LatitudeTextBoxWidth
LongitudeLabelWidth
LongitudeLabelText
LongitudeUnitText
LongitudeTextBoxWidth
使用LatLong
控件的XAML如下所示:
<Window ...>
<LatLong LatitudeLabelText="Latitude (in degrees)"
LatitudeUnitText=""
LongitudeLabelText="Longitude (in degrees)"
LongitudeUnitText=""
/>
</Window>
但這似乎是很多工作,因為我必須為每個實例重新公開每個依賴項屬性。 我想知道是否有一種更簡單的方法來公開TextBoxWithLabelAndUnits
實例本身,因此我可以直接在其上編輯屬性並使用如下所示的XAML:
<Window ...>
<LatLong Latitude.LabelText="Latitude (in degrees)"
Latitude.UnitText=""
Longitude.LabelText="Longitude (in degrees)"
Longitude.UnitText=""
/>
</Window>
因此,換句話說,我沒有公開用戶控件內每個自定義控件的屬性,而是公開了自定義控件本身並使用點表示法訪問所有屬性。
有人知道在WPF中是否可以這樣做?
當然-為什么您不只給LatLong
這兩個屬性?
public TextBoxWithLabelAndUnits LatitudeControl
{
get { return latitude; }
}
public TextBoxWithLabelAndUnits LongitudeControl
{
get { return longitude; }
}
然后可以通過代碼直接訪問它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.