1

I have a MVVM application and I want to keep track of the focused element in my ViewModel.

I would like to set ViewModel property when a TextBox is focused. I would like to have something like this

 <TextBox Text="{Binding P1}">
    <TextBox.Style>
       <Style TargetType="{x:Type TextBox}">
         <Style.Triggers>
           <Trigger Property="IsFocused" Value="True">
             <Setter Property="{Binding P1Selected}" Value="True"/>
           </Trigger>
         </Style.Triggers>
       </Style>
    </TextBox.Style>
 </TextBox>

where P1 and P1Selected are viewModel properties. Of course this code does not work. I wrote it just to give the idea..

Roberto
  • 504
  • 11
  • 23

1 Answers1

-5

Sorry, my answer was wrong. Please see OneWayToSource binding from readonly property in XAML.

Community
  • 1
  • 1
LPL
  • 16,827
  • 6
  • 51
  • 95
  • 3
    You cannot bind IsFocused. Because it is a readonly property. – Amit Mar 28 '12 at 11:46
  • 2
    You still cannot do that. see http://stackoverflow.com/questions/658170/onewaytosource-binding-from-readonly-property-in-xaml – Amit Mar 28 '12 at 11:57
  • "'IsFocused' property is read-only and cannot be set from markup." Both for OneWay and for OneWayToSource – Roberto Mar 28 '12 at 11:58
  • 1
    I have solved the problem thanks to @Amit link and in particular [this answer](http://stackoverflow.com/a/7227295/754357) – Roberto Mar 28 '12 at 12:50