1

I've following code in my WPF app.I'm trying to set the focus on a textbox on a button click...i.e. on invoking Check() method...

Code:

MainWindowResources.xaml

 <Style TargetType="TextBox" x:Key="MyFiedlStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=FocusOnMyField}" Value="true">
                <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=myField}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

MainWindow.xaml

<TextBox HorizontalAlignment="Left" Margin="1,1,0,0" Width="180" Text="{Binding MyAttributes.MyFieldValue}" TabIndex="4" Grid.Row="3" VerticalAlignment="Top" Grid.Column="5" 
                     Name="myField" Style="{DynamicResource MyFieldStyle}"/>

MainWindowViewModel

 private bool FocusOnMyField
        {
            get { return m_FocusOnMyField; }
            set
            {
                m_FocusOnMyField = value;
                OnPropertyChanged("FocusOnMyField");
            }
        }




private void Check()
{
    this.FocusOnSecDescription = true;

}

Thanks.

Vineet v
  • 175
  • 2
  • 13
  • 1. There are 2 types of focus to be aware of, are you aware of that? 2. what's `FocusOnSecDescription` ? does it have anything to do with anything else ? – Noctis Oct 12 '16 at 22:19
  • @Quantic i think he's talking about ViewModel, not VirtualMachine :) – Noctis Oct 12 '16 at 22:28
  • 1
    @Noctis Ah, yea I think you're right. Removed that comment which is probably very confusing heh. – Quantic Oct 12 '16 at 22:30
  • Your property is private. As far as I know databinding only works for public properties. – Nathan Oct 12 '16 at 23:01
  • You should probably take a look at this http://stackoverflow.com/questions/2204063/wpf-set-focus-when-a-button-is-clicked-no-code-behind – Travis Oct 13 '16 at 04:15
  • Thanks very much Nathan.My bad..yes,after the making the property public it started working! – Vineet v Oct 14 '16 at 14:43

0 Answers0