我覺得喬恩的給你答案。但是,您可能需要考慮使用基於ContentControl
而不是UserControl
的模板控制。這裏有一個10的啓動器。
將一個新的Silverlight模板控制項目添加到您的項目中調用其「LabeledControl」。
修改cs文件創建,以便它看起來像這樣: -
public class LabeledControl : ContentControl
{
public LabeledControl()
{
this.DefaultStyleKey = typeof(LabeledControl);
}
#region public string Label
public string Label
{
get { return GetValue(LabelProperty) as string; }
set { SetValue(LabelProperty, value); }
}
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register(
"Label",
typeof(string),
typeof(LabeledControl),
new PropertyMetadata(null));
#endregion public string Label
}
你會注意到所有我們所做的是改變類被繼承到ContentControl
並添加了一個「標籤」依賴屬性。
現在打開Themes/Generic.xaml文件並找到該控件的默認樣式。與此替換它: -
<Style TargetType="local:LabeledControl">
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:LabeledControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="{TemplateBinding Label}" />
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Cursor="{TemplateBinding Cursor}"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
所有我們在這裏所做的是複製了原有的風格爲ContentControl
包好了Grid
,所以我們可以把我們綁定到Label
財產TextBlock
。
您現在可能已經注意到,這些都沒有解決您想要解決的「問題」。我只是指出了創建複合控件的更多「Silverlight-esq」方法,它更加靈活。
對於您的具體問題,我會說它其實不是一個問題,在Jon的最後一個示例中,他展示瞭如何構建代碼以避免重複。通過上述控制代碼將如下所示: -
var lc = new LabeledControl
{
Label = "Check this:",
Content = new CheckBox {IsChecked = True}
};
或在XAML: -
<local:LabeledControl Label="Check this:">
<CheckBox IsChecked="True" />
</local>
由於vcsjones使用泛型類使用指出喬恩並不是很Silverlight的友好的方式,我會避免這一點。
如果在XAML標記中使用XAML,則Silverlight的XAML無法處理控件中的泛型。當然,XAML 2009支持泛型 - 但只是作爲寬鬆的文檔。如果這只是代碼,那麼泛型肯定會起作用。 – vcsjones
@vcsjones:看起來這是從XAML以外的代碼隱藏中使用,但您的觀點已被採納。 –
使用動態的第一個想法將解決問題,另一個想法並不是非常有用,因爲XAML不允許泛型,最後一個並不能解決問題,即如何在讀/寫控件的屬性之後它是由XAML創建的。 – Jaider