我有点问题。我想在我的DataGrid中有两个goupstyles。 每个组的标题都有自己的模板。选择由DataTemplateSelector确定。
问题是,当我到达选择器时,我的项目总是等于null。我希望我的绑定项(选择器中的类型是“DataReductionK”)可以返回好的模板。
谢谢!
public class ControlGroupStyleSelector : DataTemplateSelector
{
public DataTemplate StepTemplate
{ get; set; }
public DataTemplate MotherTemplate
{ get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
ContentPresenter cp = container as ContentPresenter;
if (cp != null)
{
CollectionViewGroup cvg = cp.Content as CollectionViewGroup;
if (cvg != null)
{
if (cvg.Name.ToString().Contains("K"))
{
return MotherTemplate;
}
else
{
return StepTemplate;
}
}
}
return base.SelectTemplate(item, container);
}
<DataGrid ItemsSource="{Binding DataReductionsK, Mode=OneWay}" AutoGenerateColumns="False" HorizontalContentAlignment="Center" >
<DataGrid.Resources>
<DataTemplate x:Key="StepTemplate">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Background="Transparent">
<Label>Passage n°</Label>
<Label FontWeight="Bold" FontSize="12" Content="{Binding Name}" />
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="MotherTemplate">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Background="Transparent">
<Label FontSize="10" Content="Control Name : " />
<Label FontSize="10" Content="{Binding Name}" />
</StackPanel>
</DataTemplate>
<selector:ControlGroupStyleSelector x:Key="ControlGroupStyleSelector" MotherTemplate="{StaticResource MotherTemplate}" StepTemplate="{StaticResource StepTemplate}" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="85" Header="Type" Binding="{Binding ControlType}"/>
<DataGridTextColumn Width="120" Header="Nom du contrôle" Binding="{Binding CB_Sample}"/>
<DataGridTextColumn Width="85" Header="CB Dilution" Binding="{Binding CB_Dilution}"/>
<DataGridTextColumn Width="65" Header="Dilution" Binding="{Binding Dilution}"/>
<DataGridTextColumn Width="200" Header="Range d'acceptation (mlU/mL)" Binding="{Binding AcceptableRange}"/>
<DataGridTextColumn Width="85" Header="RLU" Binding="{Binding Step.RLU}"/>
<DataGridTextColumn Width="85" Header="Conc brute (mlU/mL)" Binding="{Binding Step.RawConcentration.DisplayValue}"/>
<DataGridTextColumn Width="85" Header="Conc calculée (mlU/mL)" Binding="{Binding Step.ComputedConcentration.DisplayValue}"/>
<DataGridTextColumn Width="50" Binding="{Binding Step.CV}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="CV (%)" Width="40" TextWrapping="Wrap"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Width="85" Header="Final result (mlU/mL)" Binding="{Binding Step.FinalRes.DisplayValue}" FontWeight="Bold" />
<DataGridTextColumn Width="85" Header="Status" Binding="{Binding Step.Action}"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<!--<GroupStyle HeaderTemplateSelector="{StaticResource ControlGroupStyleSelector}"/>-->
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" HeaderTemplateSelector="{StaticResource ControlGroupStyleSelector}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>