0

I've got a custom control CheckableItemsControl that I've thrown together, it's lookless, has no dependency properties of its own and just derives from ItemsControl.

Following Rachel's answer in this question: How can I overwrite my ListBox's ItemTemplate and still keep the DisplayMemberPath? I've managed to come up with the following style in Generic.xaml:

<Style x:Key="{x:Type controls:CheckableItemsControl}" TargetType="{x:Type controls:CheckableItemsControl}"
       BasedOn="{StaticResource {x:Type ItemsControl}}">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked}" Content="{TemplateBinding ContentPresenter.Content}" 
                          ContentTemplateSelector="{TemplateBinding ContentPresenter.ContentTemplateSelector}"
                          Margin="5" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:CheckableItemsControl}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom"
                                Grid.Row="0"
                                Margin="0,0,0,3">
                        <Button x:Name="PART_SelectAllButton" Content="Select All" Width="60" Margin="5,0,5,0" />
                        <Button x:Name="PART_InvertButton" Content="Invert" Width="60" Margin="5,0,5,0" />
                    </StackPanel>

                    <Border Grid.Row="1" BorderBrush="{x:Static SystemColors.ActiveBorderBrush}" BorderThickness="1">
                        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And using it is like this:

<controls:CheckableItemsControl ItemsSource="{Binding Reports}" DisplayMemberPath="ReportName" />

Now, this works just fine at design time, the designer correctly renders the items with their ReportName being displayed as text.

At runtime however, it just renders the type name like you'd expect with no content template.

I can't work out why this would fail at runtime.

Community
  • 1
  • 1
Clint
  • 6,133
  • 2
  • 27
  • 48
  • Take a look at the output window and see if there are any errors occurring. – CalebB Jul 16 '15 at 13:57
  • @CalebB Thanks! I'm getting `Both 'ContentTemplate' and 'ContentTemplateSelector' are set; 'ContentTemplateSelector' will be ignored. ContentPresenter:'ContentPresenter' (Name='')` but I'm not setting ContentTemplate... – Clint Jul 16 '15 at 13:59
  • @CalebB found the reason! http://stackoverflow.com/questions/15654935/wpf-debug-error-output-system-windows-data-error-25 – Clint Jul 16 '15 at 13:59
  • Cheers, glad you found the solution. :) – CalebB Jul 16 '15 at 14:00
  • @CalebB Just annoying, because it means I'll need to add my own way of lifting out the value without setting `ContentTemplate` *sigh* – Clint Jul 16 '15 at 14:01
  • Well it's a start and your on the right track now so happy coding! :) – CalebB Jul 16 '15 at 14:06

1 Answers1

0

Found out that it's down to this:

wpf debug error output System.WIndows.Data Error 25

Ultimately ContentTemplateSelector is being ignored because I've implicitly adopted a ContentTemplate by using DisplayMemberPath.

The solution will likely be adding a custom dependency property to pull out the value manually.

Further information:

From the ContentControl.ContentTemplateSelector Property page on MSDN:

If both the ContentTemplateSelector and the ContentTemplate properties are set, then this property is ignored.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
Clint
  • 6,133
  • 2
  • 27
  • 48