I have a Custom Control:
I don't want to get into specifics so for simplicity's sake i have 3 Dependency Properties :
MyCustomControl (CS) :
public class MyCustomControl : Control
{
DP Value1
DP InternalValue
DP SelectedValue
OnValue1Changed()
{
InternalValue = CalculateBasedOn1();
}
static bool _isSetInternally;
OnInternalValueChanged()
{
if(Condition())
{
_isSetInternally = true;
SelectedValue = e.NewValue;
}
else
{
Value1 = FixValue1();
}
}
OnSelectedValueChanged()
{
if(_isSetInternally)
{
_isSetInternally = false;
return;
}
Value1 = ExtractValue1FromInput(e.NewValue);
}
public List<string> Values
{
get{ return new List<string>() { "1","2",......,"200"};}
}
}
My ControlTemplate (Again Simplified) :
<ControlTemplate>
<ComboBox x:Name="cb"
ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, Path=Values}"
SelectedItem={Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
</ControlTemplate>
The Problem : cb is showing the last value chosen , even after it was fixed as explained below .
Flow :
1) Input :
1.1) SelectedValue is bound to a property in my DataContext , it receives a value.
1.2) OnSelectedValueChanged() sets Value1.
1.3) Value1 Sets "cb" SelectedItem via binding.
1.4) OnValue1Changed sets InternalValue.
1.5) OnInternalValueChanged() flags _isSetInternally = true and Updates SelectedValue.
1.6) OnSelectedValueChanged() zeros _isSetInternally = false and stops flow (return).
2) Output :
2.1) cb's SelectedItem is changed.
2.2) Value1 is set via Binding.
2.3) OnValue1Changed() sets InternalValue.
2.4) If Condition is met propagate Output.
2.4.1) go to (1.4)
2.4.2) THE PROBLEM , Condition was not met, Set Value1 again with a valid value.
2.5) go to (1.4)
The Problem in 2.4.2 is that The ComboBox is still showing the non - valid value chosen in (2.1)
Observing with snoop i can see that the SelectedItem is correct and have been changed , but the SelectedValue and SelectedIndex are still the one's chosen before the Fix.
*Further more iv'e attempted to Coerce Value1 on a Coercion Callback , it had the same effect.
Any idea's why the ComboBox doesn't update it's Value via Binding in this scenario ?