Search code examples
windows-8winrt-xamlxaml-binding

Binding to a control's FocusState in Windows 8 Metro Style Application


I'm using the following code to bind the RichTextBlock.Visibility property to another control's FocusState property.

 <RichTextBlock FontFamily="Segoe UI" FontSize="22" FontWeight="Light" 
                               Foreground="{StaticResource SwiftLightTextBrush}" 
                               Visibility="{Binding ElementName=ProfessionalHeadlineInput, Path=FocusState, Converter={StaticResource FocusStateToVisibilityConverter}}" >

The implentation of FocusStateToVisibilityConverter.Convert is the following:

public object Convert(object value, Type targetType, object parameter, string language)
{
    var focusState = (FocusState)value;

    return focusState == FocusState.Keyboard || focusState == FocusState.Pointer || focusState == FocusState.Programmatic ? Visibility.Visible : Visibility.Collapsed;
}

It may be a novice question, but why the binding is not applied when I'm 'focusing' onto a target element (ProfessionalHeadlineInput is a TextBox element) using the mouse or tab navigation?

I've inserted the breakpoint into the Convert method, but it is not called when I'm clicking or 'tabbing' onto the ProfessionalHeadlineInput TextBox.

NB The important part - my project is a Windows 8 Metro Style Application.


Solution

  • I've got an answer from Matt Small on Microsoft forum:

    OK - this is actually a bug with FocusState - it's not updating the value. I'm filing a bug in our database. Thank you for brining this up.