I've a xaml screen in my C#/WPF app having a combobox(ProductType) and a textbox(ProductCode). When the screen loads for the first time,I'm setting the focus this Textbox and its working fine using my code below. I also need to set the focus when user changes a value in the comboxbox but it does not seem to work.
What am I missing here please? (Note:My first preference would be to achieve a solution for this using MVVM design pattern.If it does not work,I would like to go for code-behind approach please.)
MainWindowResources.xaml
<Style TargetType="TextBox" x:Key="ProductCodeStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding FocusOnProductCode}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
</DataTrigger>
</Style.Triggers>
</Style>
MainWindow.xaml:
<TextBox Name="txtProductCode" HorizontalAlignment="Left" Height="22" TextWrapping="Wrap" Text="{Binding ProductCodeValue, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged }"
VerticalAlignment="Top" Width="165" Style="{DynamicResource ProductCodeStyle}" Grid.Column="3" Margin="1,2,0,0" TabIndex="0" IsHitTestVisible="True"/>
MainWindowViewModel.cs
public MainWindowViewModel(MainWindow window)
{
this.FocusOnProductCode = true;
}
public ProductType SelectedProductType
{
get
{
return m_selectedProductType;
}
set
{
m_selectedProductType = value;
this.FocusOnProductCode = true;
}
}
public bool FocusOnProductCode
{
get { return m_focusOnProductCode; }
set
{
m_focusOnProductCode;= value;
OnPropertyChanged("FocusOnProductCode");
OnPropertyChanged("SelectedProductType");
}
}
Thanks.