2011-06-20 29 views
1

我想在Silverlight創建一個FilterControl: [字幕] [文本框] [清除鍵]FilterControl - 用戶自定義控件?

我想創建爲styleabel控制,具有清除按鈕的功能(我不想將其留給其他開發人員)以及屬性(Caption,FilterText)。

但據我所知,第一個是自定義控件,第二個和第三個是用戶控件功能。

是否可以創建類似的東西?


這裏是CC的代碼:

<!-- Built-In Style for FilterControl --> 
<Style TargetType="Controls:FilterControl"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <!-- ControlTemplate --> 
      <ControlTemplate TargetType="Controls:FilterControl"> 
       <!-- Template's Root Visual --> 
       <Grid x:Name="LayoutRoot"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="{TemplateBinding Height}"/> 
        </Grid.RowDefinitions> 

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 

         <!--FilterCaption PART--> 
         <TextBlock x:Name="FilterCaptionTextBlock" Text="{TemplateBinding FilterCaption}" VerticalAlignment="Center" /> 

         <!--FilterTextBox PART--> 
         <TextBox x:Name="FilterTextBox" 
           HorizontalAlignment="Center" Text="{Binding FilterText, Mode=TwoWay}" IsEnabled="{Binding IsEnabled}" VerticalAlignment="Center" /> 
         <!--<TextBox x:Name="FilterTextBox" 
           HorizontalAlignment="Center" Text="{TemplateBinding FilterText}" IsEnabled="{Binding IsEnabled}" VerticalAlignment="Center" />--> 

         <!--ClearFilterTextButton PART--> 
         <Button x:Name="ClearFilterTextButton" 
           Content="X" IsEnabled="{TemplateBinding IsEnabled}" VerticalAlignment="Center" /> 

        </StackPanel> 

        <!--VisualStateManager--> 
        <VisualStateManager.VisualStateGroups> 
         ... 
        </VisualStateManager.VisualStateGroups> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

回答

1

首先讓清晰的任何FrameworkControl包括UserControl然而,可以稱呼我懷疑你可能會闖民宅什麼能夠替換控件的模板。這需要一個自定義的模板控件。

你的問題的答案是:是的,當然是。

您將使用您偏好的默認模板創建自定義模板控件。

爲Caption和FilterText添加依賴屬性。

使用TemplateBinding將Captiona和Filter文本連接到默認模板中的元素。

你需要指定你的模板需要一個TextBox類型的TemplatePart。

您將編寫代碼來監視TextBox中的更改,以便控件更新其Filter屬性。

+0

多數民衆贊成清楚,我必須創建一個自定義控件。但是:1.什麼是清除按鈕?由於UC中沒有xaml,所以我無法到達按鈕,也無法添加事件處理程序。 2.雙向約束不適用於這些屬性 – Aaaaaaaa