0

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.

Vineet v
  • 175
  • 2
  • 13
  • 1
    Setting focus is a concern of the UI. Just watch the combo box in the codebehind of the window and set your focus as appropriate. –  Dec 05 '16 at 18:16
  • Thanks @Will.But I would like to see if there's any solution for this using MVVM design pattern.Have updated my question with the same. – Vineet v Dec 05 '16 at 18:37
  • Doing your UI work in the codebehind *is* using the MVVM design pattern. Shoehorning UI work into your ViewModel is the exact *opposite* of the MVVM design pattern. Don't feel bad--rejoice! You can solve this problem in only a few moments and lines of code! And you would be adhering to the pattern! It's a win-win! –  Dec 05 '16 at 19:11

1 Answers1

0

Focusing an element in pure XAML by setting the FocusManager.FocusedElement attached property in a Style setter dynamically at runtime won't work but you could use a behaviour to be able to set a Boolean property indicating whether the element should be focused in the view model class as suggested in the following threads:

Set focus on textbox in WPF from view model (C#)

How to set Focus to a WPF Control using MVVM?

<TextBox  Name="txtProductCode" Text="{Binding ProductCodeValue, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" local:FocusExtension.IsFocused="{Binding FocusOnProductCode}"/>
Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88