简体   繁体   English

将ContentControl *放在* WPF DataTemplate中?

[英]Putting a ContentControl *inside* a WPF DataTemplate?

I have a custom Expander control called SpecialExpander . 我有一个名为SpecialExpander的自定义扩展器控件。 It is basically just a standard Expander with a fancy header and a couple properties ( HeaderText and IsMarkedRead ). 它基本上只是一个标准的Expander ,带有花式头和几个属性( HeaderTextIsMarkedRead )。

I began by creating a simple class: 我从创建一个简单的类开始:

public class SpecialExpander : Expander
{
    public string HeaderText { get; set; }
    public bool IsMarkedRead { get; set; }
}

Then I created a style that sets a couple properties on the expander (eg, margins, padding, etc.) and, importantly, it also defines a custom DataTemplate for the HeaderTemplate property. 然后,我创建了一种样式,该样式在扩展器上设置了几个属性(例如,边距,填充等),并且重要的是,它还为HeaderTemplate属性定义了自定义DataTemplate The template is basically a grid with two rows. 模板基本上是具有两行的网格。

As shown in the illustrations below... 如下图所示...

  • for the top row, I'd like a fixed layout (it's always TextBlock TextBlock CheckBox ) 对于第一行,我想要一个固定的布局(它始终是TextBlock TextBlock CheckBox
  • for the bottom row, however, I want to be able to provide custom XAML for each expander. 对于底行,我希望能够为每个扩展器提供自定义XAML。

I tried putting <ContentControl Grid.Row="1" ... /> in the DataTemplate , but I couldn't figure out how to hook it up properly. 我尝试将<ContentControl Grid.Row="1" ... />放入DataTemplate ,但是我不知道如何正确地将其连接起来。


alt text http://img85.imageshack.us/img85/1194/contentcontrolwithintem.jpg 替代文字http://img85.imageshack.us/img85/1194/contentcontrolwithintem.jpg


替代文字


Question

How can I build a DataTemplate for my SpecialExpander so that the header has some fixed content (top row) and a place-holder for custom content (bottom row)? 如何为我的SpecialExpander构建一个DataTemplateSpecialExpander使标题具有一些固定内容(顶部行)和占位符用于自定义内容(底部行)?

For the second illustration, I would want to be able to do something like this: 对于第二个示例,我希望能够执行以下操作:

<SpecialExpander HeaderText="<Expander Header Text>" IsMarkedRead="True">
    <SpecialExpander.Header>
        <StackPanel Orientation="Horizontal">
            <RadioButton Content="High" />
            <RadioButton Content="Med" />
            <RadioButton Content="Low" />
        </StackPanel>
    <SpecialExpander.Header>
    <Grid>
        <Label>Main Content Goes Here</Label>
    </Grid>
</SpecialExpander>

It hit me this morning how to solve this: instead of building a SpecialExpander , I just need a normal Expander . 今天早上我SpecialExpander如何解决这个问题:我只需要一个普通的Expander ,而不是构建SpecialExpander Then, for the header, I will use a custom ContentControl called SpecialExpanderHeader . 然后,对于标题,我将使用一个名为SpecialExpanderHeader的自定义ContentControl

Here's how it works... 运作方式如下...

SpecialExpanderHeader class: SpecialExpanderHeader类:

public class SpecialExpanderHeader : ContentControl
{
    public string HeaderText { get; set; }
    public bool IsMarkedRead { get; set; }
}

SpecialExpanderHeader style: SpecialExpanderHeader样式:

<Style TargetType="custom:SpecialExpanderHeader">
    <Setter Property="Padding" Value="10" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="custom:SpecialExpanderHeader">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Row="0" Orientation="Horizontal">
                        <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=custom:SpecialExpanderHeader}, Path=HeaderText}" />
                        <CheckBox Margin="100,0,0,0" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=custom:SpecialExpanderHeader}, Path=IsMarkedRead}" />
                    </StackPanel>
                    <Separator Grid.Row="1" />
                    <ContentPresenter Grid.Row="2" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Expander style 扩展器样式

<Style x:Key="Local_ExpanderStyle" TargetType="Expander" BasedOn="{StaticResource {x:Type Expander}}">
    <Setter Property="Margin" Value="0,0,0,10" />
    <Setter Property="Padding" Value="10" />
    <Setter Property="FontSize" Value="12" />
</Style>

Usage 用法

<Expander Style="{StaticResource Local_ExpanderStyle}">
    <Expander.Header>
        <custom:SpecialExpanderHeader IsMarkedRead="True" HeaderText="Test">
            <StackPanel Orientation="Horizontal">
                <RadioButton Content="High" />
                <RadioButton Content="Medium" />
                <RadioButton Content="Low" />
            </StackPanel>
        </custom:SpecialExpanderHeader>
    </Expander.Header>
    <Grid>
        <!-- main expander content goes here -->
    </Grid>
</Expander>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM