2
votes

I have a ControlTemplate in which I would like to take in two collections and combine them into one collection which would then be bound to an ItemsControl.The calculation is done by the Calculator object, which I create an instance of within the ResourceDictionary of the template.

<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type local:IndicatorBar}">
       <ControlTemplate.Resources>
          <local:Calculator 
            x:Key="_calculator"
            Ranges="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Ranges}" 
            DataSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataSource}" />
        </ControlTemplate.Resources>

        <ItemsControl ItemsSource="{Binding Ratios, Source={StaticResource _calculator}}">
          <ItemsControl.ItemTemplate>
            <DataTemplate>
              <Border Background="{Binding Range}">
                <TextBlock Text="{Binding Ratio}" Foreground="White" />
              </Border>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
      </ControlTemplate>
    </Setter.Value>
</Setter>

However, this does not seem to work and causes binding errors:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.
BindingExpression:Path=Ranges; DataItem=null; target element is 'Calculator' (HashCode=33746798); target property is 'Ranges' (type 'Ranges')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element.
BindingExpression:Path=DataSource; DataItem=null; target element is 'Calculator' (HashCode=33746798); target property is 'DataSource' (type 'IEnumerable')

I'm unsure of how I can get around this issue, so any help would be greatly appreciated!

1
your error says the targetElement is "Calculator", yet your ControlTemplate is typed towards "local:IndicatorBar". How do these two objects relate in your tree?Bill Tarbell
There's too little code to describe the context. Please post how you use this Template (e.g. the IndicatorBar type used in XAML) and the code where you set its Ranges and DataSource property.Paweł Motyl
If you have the source to calculator then I would probably just make it as a multi value converter that takes the two collections as parameters. Then you can do your ItemsSource binding passing the converter along with it.Andy
Ranges and Datasource property in Calculator class are dependency property?D J
@BillTarbell If you see in the XAML, I have a calculator that is processing the input to the control and then the Border is bound to the output of the calculator.Vishal Mistry

1 Answers

0
votes

At first, your binding using RelativeSource.TemplatedParent mode so the source of the binding is the control that you apply this template. I don't think TemplatedParent is suitable for your case. Please refer to this link for TemplatedParent use. Could you add your model which contains your data for binding to Calculator control?