Is there a way to have element to element binding in Silverlight templated controls?
Example: I have two custom controls, SomeControl and CustomSlider. SomeControl has a dependency property called someValue. I want to bind the value of CustomSlider to this property, so my generic.xaml file looks like this:
<Style TargetType="local:SomeControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:SomeControl"> <...> </ControlTemplate> </Setter.Value> </Setter> </Style>
<Style TargetType="local:CustomSlider">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomSlider">
<Slider Value="{Binding someValue, ElementName=local:SomeControl}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and this is my dependency property:
public int someValue, { get { return (int)GetValue(someValueProperty); } set { SetValue(someValueProperty, value); } } public static readonly DependencyProperty (someValueProperty) = DependencyProperty.Register(someValue); typeof(int), typeof(SomeControl, new PropertyMetadata(0));
This throws an “BindingExpression_CannotFindElementName” exception.
You can’t use it like this. A binding through
ElementNameshould be used to specific element instance, not style. You can create other dependency property, saySliderValuein yourCustomSlidercontrol and bind to it.And change your Slider
Valuefrom template when yourSliderValueproperty changes;