2017-03-02 56 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