【问题标题】:Hierarchical templates of controls控件的分层模板
【发布时间】:2017-07-22 16:47:07
【问题描述】:

我不确定 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。那么对于一个控件来说,有多少个模板子孙呢?我们可以在图表中列举它们以及它们之间的关系吗?

【问题讨论】:

    标签: wpf xaml templates controls


    【解决方案1】:

    我在下面解释了属性的名称和它们的类型之间的区别:

    <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>
    

    希望对你有帮助。

    【讨论】:

    • 所以这适用于组合框。在一般的控件中,说如果是一个按钮,它的图案是一样的吗?
    • 不,按钮没有ItemTemplateItemContainerStyleItemsPanel。这些特定于ItemsControl 和其他从它派生的控件,例如ListBoxTreeView 等。Button 具有Template 类型为ControlTemplate 的属性。事实上,它并不需要这些属性。
    • 所以每个控件都有自己的模式。我怎样才能记住这些?
    • 与他们一起工作(生活)几周后,您将了解他们的模式。没那么复杂。 ;)
    猜你喜欢
    • 2018-03-03
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多