1

I know this wasn't right when I wrote it and I've been able to glean most of the answer for another answer, but just can't grasp the final bit. The binding does pass from the UI to the DependencyProperty (and the other way when the control is created).

My Template (need to move the IsChecked binding to the instance):

<ControlTemplate x:Key="MyHeaderedContentTemplate" TargetType="HeaderedContentControl">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <ContentControl>
        <StackPanel Orientation="Horizontal">
            <CheckBox Margin="2,2,20,2"
                                    Content="All/None"
                                    IsChecked="{Binding Path=AllFeatureTypesChecked, Mode=TwoWay}" />
            <TextBlock Text="{TemplateBinding Header}" Margin="2"/>
        </StackPanel>
    </ContentControl>
    <ContentControl Grid.Row="1" Content="{TemplateBinding Content}" />
</Grid>

The Instance:

<HeaderedContentControl Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="4"
                    Template="{StaticResource  ResourceKey=MyHeaderedContentTemplate}"
                    Header="Feature Details by Type">
    <HeaderedContentControl.Resources>

    </HeaderedContentControl.Resources>
    <ListBox ItemTemplate="{StaticResource SelectableLinkNode}"
            ItemsSource="{Binding Features}"/>
</HeaderedContentControl>

The Content Binding provides a list of CheckBox items

And the Setter (of course, there's a Boolean AllFeatureTypesChecked DependencyProperty):

    /// <summary>
    /// Needs to be set on Startup and on ItemIsCheckedChanged Event from the Features List
    /// </summary>
    private void SetAllSelectedState()
    {
        bool allSelected = (Features.Count > 0);
        foreach (var CheckableItem in Features) {
            if (CheckableItem.IsChecked == false) {
                allSelected = false;
                break;
            }
        }

        SetCurrentValue(AllFeatureTypesCheckProperty, allSelected);

    }

For reference, here's the DP

public static readonly DependencyProperty AllFeatureTypesCheckProperty =
        DependencyProperty.Register("AllFeatureTypesCheck", typeof(Boolean), typeof(ReportSources),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAllFeatureTypesCheckedChanged));

This is really fun stuff and I couldn't do it without the awesome folks here on SO! Thanks!

UPDATE: OK, Now I've got this (brainstorm):

    <ControlTemplate x:Key="MyHeaderedContentTemplate" TargetType="HeaderedContentControl">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <ContentControl>
                <StackPanel Orientation="Horizontal">
                <ContentPresenter Content="{DynamicResource ResourceKey=CheckControl}" Margin="2,2,20,2"/>
                    <TextBlock Text="{TemplateBinding Header}" Margin="2"/>
                </StackPanel>
            </ContentControl>
            <ContentControl Grid.Row="1" Content="{TemplateBinding Content}" />
        </Grid>
    </ControlTemplate>

instantiated like so:

    <HeaderedContentControl Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="4"
                            Template="{StaticResource  ResourceKey=MyHeaderedContentTemplate}"
                            Header="Feature Details by Type"
                            >
        <HeaderedContentControl.Resources>
            <CheckBox x:Key="CheckControl"
                      Content="All/None"
                      IsThreeState="True"
                      IsChecked="{Binding Path=AllFeatureTypesChecked, Mode=TwoWay}"
                      />
        </HeaderedContentControl.Resources>
        <ListBox ItemTemplate="{StaticResource SelectableLinkNode}"
                 ItemsSource="{Binding Features}"/>
    </HeaderedContentControl>

but still can't get the value set on the DP to show on the UI after the control has been created.

...sure could use a little help here, Thanks.

Community
  • 1
  • 1
Barton
  • 547
  • 5
  • 11

1 Answers1

1

Silly, really - or quite dumb... But I blame the use of strings for registration. The IDE just doesn't do enough to try to update such things when an identifier's spelling is changes.

    public Boolean? AllFeatureTypesChecked
    {
        get { return (Boolean?) GetValue(AllFeatureTypesCheckedProperty); }
        set { SetValue(AllFeatureTypesCheckedProperty, value); }
    }

    #region Using a DependencyProperty as the backing store for AllFeatureTypesCheck.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AllFeatureTypesCheckedProperty =
        DependencyProperty.Register("AllFeatureTypesCheck", typeof(Boolean?), typeof(ReportSources),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAllFeatureTypesCheckedChanged));
    #endregion

Notice the spelling on the Object's property vs the regsistered name on the DP.

Barton
  • 547
  • 5
  • 11
  • So now I've got ReSharper installed. I'm anxious to see if what I've heard about this product is true. – Barton Jan 25 '13 at 04:46
  • Well, R# is a nice addition to my arsenal, but it didn't handle this: RaisePropertyChanged("WaasStatus") – Barton Feb 10 '13 at 22:16
  • Well, it turns out that if you try to use the VS refactoring that pops up when you rename an identifier, R# doesn't get the opportunity to scan for matching text. The R# section of the context menu offers "Rename" that has the option for searching string literals. – Barton Mar 23 '13 at 01:53