Search code examples
c#xamlwindows-store-appsuwpuwp-xaml

Textbox is becoming readonly in flyout, UWP store app


In button flyout I am using one usercontrol inside that I have textbox. when running the app the textbox is appearing as readonly, don't know why I am getting this issue. nowhere I am setting readonly.

<TextBox Margin="2" Height="32" 
                     MaxHeight="60"
                     TextWrapping="Wrap"
                     HorizontalAlignment="Stretch" 
                     TextAlignment="Left"
                     Text="ramesh"
                     Style="{x:Null}"/>

Solution

  • Figure out the issue it's because of anniversary update.

    https://blogs.msdn.microsoft.com/wsdevsol/2016/09/14/combobox-from-an-appbarbutton-loses-mouse-input-on-1607/

    I created attached property for solution given in above link. Below is the attached property

    public class CompatExtensions
    {
        public static bool GetAllowFocusOnInteraction(DependencyObject obj)
        {
            return (bool)obj.GetValue(AllowFocusOnInteractionProperty);
        }
        public static void SetAllowFocusOnInteraction(DependencyObject obj, bool value)
        {
            obj.SetValue(AllowFocusOnInteractionProperty, value);
        }
        // Using a DependencyProperty as the backing store for AllowFocusOnInteraction.  
        // This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AllowFocusOnInteractionProperty =
            DependencyProperty.RegisterAttached("AllowFocusOnInteraction", 
                                                typeof(bool),
                                                typeof(CompatExtensions),
                                                new PropertyMetadata(0, AllowFocusOnInteractionChanged));
    
        private static bool allowFocusOnInteractionAvailable = 
            Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent(
                "Windows.UI.Xaml.FrameworkElement", 
                "AllowFocusOnInteraction");
        private static void AllowFocusOnInteractionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (allowFocusOnInteractionAvailable)
            {
                var element = d as FrameworkElement;
                if (element != null)
                {
                    element.AllowFocusOnInteraction = (bool)e.NewValue;
                }
            }
        }
    }
    

    And an example of it used:

    <AppBarButton local:CompatExtensions.AllowFocusOnInteraction="True" Icon="Setting">
        <AppBarButton.Flyout>
            <Flyout>
                <StackPanel Orientation="Vertical" >
                    <ComboBox>
                        <ComboBoxItem Content="Red" IsSelected="True" />
                        <ComboBoxItem Content="Green" />
                        <ComboBoxItem Content="Blue"/>
                    </ComboBox>
                </StackPanel>
            </Flyout>
        </AppBarButton.Flyout>
    </AppBarButton>