I'm trying to focus different elements of control when it's created, based on some variables in view model. For this I registered an additional property in code behind and bound it:
public static readonly DependencyProperty FocusArticlesOnStartProperty = DependencyProperty.Register("FocusArticlesOnStart",
typeof(bool), typeof(ChargeCreate), new PropertyMetadata(false));
public bool FocusArticlesOnStart
{
get => (bool)GetValue(FocusArticlesOnStartProperty);
set => SetValue(FocusArticlesOnStartProperty, value);
}
In XAML:
<UserControl.Style>
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="FocusArticlesOnStart">
<Setter.Value>
<MultiBinding Converter="{g:ToBoolean}" ConverterParameter="p0|!p1">
<MultiBinding.Bindings>
<Binding Path="FocusOnStart" />
<Binding Path="HasOrderString" />
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</UserControl.Style>
Then I try to use this property in a method, invoked by Loaded event in code behind:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (FocusArticlesOnStart)
{
articleComboBox.Focus();
}
else
{
orderStringTextBox.Focus();
}
}
However, on the moment of Loaded the property is false. Though FocusOnStart and HasOrderString are set (as well as corresponding OnPropertyChange's invoked) before creation of this control, converter's MultiConvert method is only invoked after this method. According to documentation here,
Standard data binding (binding to local sources, such as other properties or directly defined data sources) will have occurred prior to Loaded.
Why doesn't it apply to my case? Where (in which method or after which event) should I be able to obtain correct FocusArticlesOnStart in order to set initial focus, when control is created?
