我有一个名为SpecialExpander
的自定义Expander控件。它基本上只是一个标准Expander
,带有花哨的标题和几个属性(HeaderText
和IsMarkedRead
)。
我开始创建一个简单的类:
public class SpecialExpander : Expander
{
public string HeaderText { get; set; }
public bool IsMarkedRead { get; set; }
}
然后我创建了一个在扩展器上设置一些属性的样式(例如,边距,填充等),更重要的是,它还为DataTemplate
属性定义了一个自定义HeaderTemplate
。模板基本上是一个有两行的网格。
如下图所示......
TextBlock
TextBlock
CheckBox
)我尝试将<ContentControl Grid.Row="1" ... />
放入DataTemplate
,但我无法弄清楚如何将其正确连接。
alt text http://img85.imageshack.us/img85/1194/contentcontrolwithintem.jpg
alt text http://img29.imageshack.us/img29/1194/contentcontrolwithintem.jpg
问题
如何为DataTemplate
构建SpecialExpander
,以便标题包含一些固定内容(顶行)和自定义内容的占位符(底行)?
对于第二个例子,我希望能够做到这样的事情:
<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>
答案 0 :(得分:1)
今天早上我找到了如何解决这个问题:我只需要一个正常的SpecialExpander
,而不是建立一个Expander
。然后,对于标题,我将使用名为ContentControl
的自定义SpecialExpanderHeader
。
以下是它的工作方式......
SpecialExpanderHeader类:
public class SpecialExpanderHeader : ContentControl
{
public string HeaderText { get; set; }
public bool IsMarkedRead { get; set; }
}
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>
扩展器样式
<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>
<强>用法强>
<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>