2017-03-02 44 views
1

我不確定wpf控件的分層模板。例如,對於組合框。控件的分層模板

<ComboBox ItemsSource="{Binding .}"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <Border HorizontalAlignment="Stretch"> 
       <Border.Style> 
        <Style TargetType="Border"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}, Path=DataContext.IsSelected}" Value="True"> 
           <Setter Property="Background" Value="LightGreen"/> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Border.Style> 
       <StackPanel HorizontalAlignment="Stretch"> 
        <TextBlock Text="{Binding Name}"/> 
        <TextBlock Text="{Binding Email}"> 
        </TextBlock> 
       </StackPanel> 
      </Border> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

它可能有控制模板類似,但我不知道。

combox有一個模板兒子ItemTemplate和一個孫子DateTemplate。因此,爲了控制,有多少模板兒子和孫子?我們可以枚舉它們和圖表中的關係嗎?

回答

0

我解釋屬性及其類型在以下的名之間的區別:

<ComboBox> 
    <ComboBox.ItemTemplate> 
      <!-- Add a DataTemplate here. It determines how the content 
       of your data item appears. Use it to bind data 
       fields or format string and etc --> 
    </ComboBox.ItemTemplate> 
    <ComboBox.ItemContainerStyle> 
      <!-- Add a Style here. It determines the appearance of the 
       element (e.g., ComboBoxItem) that contains the data. Set 
       Selection behaviour, Background color, etc--> 
    </ComboBox.ItemContainerStyle> 
    <ComboBox.Template> 
      <!-- Add a ControlTemplate here. It contains the tree of 
       elements that define the desired look. Of course, most of 
       the time, you do not set this property and use the default 
       value. Use it for example to change the shape of the ToggleButton.--> 
    </ComboBox.Template> 
    <ComboBox.ItemsPanel> 
      <!-- Add an ItemsPanelTemplate here. Use it to determine the 
       the panel that the ItemsPresenter creates for the Items. 
       You can change the orientation of the ComboBoxItems here .--> 
    </ComboBox.ItemsPanel> 
</ComboBox> 

希望它幫助。

+0

所以這對於組合框。在一般控制中,如果它是一個按鈕,它是否具有相同的模式? – Bigeyes

+0

不,一個按鈕沒有「ItemTemplate」,「ItemContainerStyle」和「ItemsPanel」。這些特定於[''ItemsControl''](https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol(v = vs.110).aspx)和其他控件派生自它,比如''ListBox'',''TreeView''等''Button''具有''ControlTemplate''類型的'''Template''屬性。實際上,它不需要這些屬性。 – Ron

+0

所以每個控件都有自己的模式。我怎樣才能記住這些? – Bigeyes