2015-01-04 80 views
0

我將使用一組控件,每個控件都由一個標籤和一個文本框組成。宣佈自定義控件是否是一個好主意,以便將標籤的標題和文本框的輸入區域封裝爲一個整體?聲明自定義控件

我可以在C#中繼承和創建一些東西,但這裏的重點是要在XAML中變得更好,所以通過標記聲明這樣的東西(不知道它是否稱爲資源,模板,樣式或其他)是優選的(如果還沒有必要的話)。

目前,我採用了以下方法,但我不確定未來是否會爲自己創造更多的頭痛。

<StackPanel Grid.Row="0" Grid.Column="0"> 
    <Label Content="Boom" Style="{StaticResource DefaultInputStyle}" /> 
    <DatePicker Style="{StaticResource DefaultInputStyle}" /> 
</StackPanel> 

這個想法最好能夠使用類似的東西。

目前,我採用了以下方法,但我不確定未來是否會爲自己創造更多的頭痛。

<MyCoolControl Grid.Row="0" Grid.Column="0" 
       Content="Boom" 
       Style="{StaticResource DefaultInputStyle}" /> 
+0

我有沒有這樣的MVC的,但會你也可以在wpf中使用。 – Saravanan 2015-01-04 11:05:12

+0

MVC創建一個HTML標記並在客戶端動態設置它。 WPF能夠編譯時間,所以我不確定這些是如何翻譯的。你能否給出一個關於如何聲明如此複雜控制的例子?或建議在評論關鍵詞集中? – 2015-01-04 11:10:40

+2

注意:我想你是在討論一個'UserControl'而不是'custom control'。對於區別請參閱:http://wpftutorial.net/CustomVsUserControl.html – 2015-01-04 11:58:52

回答

0

我不確定您是否可以爲Label和DatePicker設置DefaultInputStyle。什麼是DefaultInputStyle的TargetType?如果您將在多個應用程序中使用此自定義控件,建議使用自定義控件。如果你想創建自定義控件,你需要繼承控件,創建一些依賴屬性,重寫DefaultStyleKeyProperty。

public class MyCoolControl : Control 
{ 
    public Style LabeStyle 
    { 
    get { return (Style)GetValue(LabeStyleProperty); } 
    set { SetValue(LabeStyleProperty, value); } 
    } 

    public static readonly DependencyProperty LabeStyleProperty = 
    DependencyProperty.Register(
     "LabeStyle", typeof(Style), typeof(MyCoolControl)); 

    public Style DatePickerStyle 
    { 
    get { return (Style)GetValue(DatePickerStyleProperty); } 
    set { SetValue(DatePickerStyleProperty, value); } 
    } 

    public static readonly DependencyProperty DatePickerStyleProperty = 
    DependencyProperty.Register(
     "DatePickerStyle", typeof(Style), typeof(MyCoolControl)); 

    public object LabelContent 
    { 
    get { return (object)GetValue(LabelContentProperty); } 
    set { SetValue(LabelContentProperty, value); } 
    } 

    public static readonly DependencyProperty LabelContentProperty = 
    DependencyProperty.Register(
     "LabelContent", typeof(object), 
     typeof(MyCoolControl), new PropertyMetadata(null)); 

    static MyCoolControl() 
    { 
    DefaultStyleKeyProperty.OverrideMetadata(
     typeof(MyCoolControl), 
     new FrameworkPropertyMetadata(typeof(MyCoolControl))); 
    } 
} 

定義隱含風格MyCoolControl在主題/ Generic.xaml:

<Style TargetType="local:MyCoolControl"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <StackPanel> 
        <Label Content="{TemplateBinding LabelContent}" Style="{TemplateBinding LabeStyle}" /> 
        <DatePicker Style="{TemplateBinding DatePickerStyle}" /> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

然後你可以使用自定義控制:

<local:MyCoolControl Grid.Row="0" Grid.Column="0" 
      LabelContent="Boom" DatePickerStyle="{StaticResource DefaultInputDatePickerStyle}" 
      LabelStyle="{StaticResource DefaultInputLabelStyle}" /> 
+0

至於你的問題*什麼是DefaultInputStyle的TargetType?* - 我騙了。我把* TargetType * * UserControl *,嘿嘿。只是爲了這個例子。至於你的回覆 - 這是否意味着我**不能從XAML那裏做到這一點? ID。我**必須**繼承和玩C#和代碼後面? (我想掌握XAML,是這個想法,C#我已經被覆蓋了,非常好) – 2015-01-04 12:16:06

+0

如果你編寫自定義控件,你必須一起玩C#(VB)和XAML。在你的情況下,使用你的自定義控件要容易得多。 – user2250152 2015-01-04 12:26:56