using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Data; using System; using System.ComponentModel; using System.Runtime.InteropServices; namespace App1 { internal partial class FlowDirectionManager : DependencyObject, INotifyPropertyChanged { public FlowDirection FlowDirection { get { return (FlowDirection)GetValue(FDTargetProperty); } set { SetValue(FDTargetProperty, value); } } public static readonly DependencyProperty FDTargetProperty = DependencyProperty.Register("FlowDirection", typeof(FlowDirection), typeof(FlowDirectionManager), new PropertyMetadata(FlowDirection.LeftToRight)); public Binding CreateBindingToFlowDirection(FrameworkElement target) { var binding = new Binding { Source = this, Path = new PropertyPath("FlowDirection"), Mode = BindingMode.OneWay }; target.SetBinding(FrameworkElement.FlowDirectionProperty, binding); return binding; } public void SetFlowMode(IntPtr hwnd, FlowDirection fd) { // Hard coded for testing, should use original value var niStyle = 0x0000000000000100; if (fd == FlowDirection.RightToLeft) { niStyle |= WS_EX_LAYOUTRTL; } SetWindowLongPtr(hwnd, GWL_EXSTYLE, niStyle); SetProcessDefaultLayout(fd == FlowDirection.LeftToRight ? (uint)0 : 1); FlowDirection = fd; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FlowDirection")); } // Native APIs const int GWL_EXSTYLE = -20; const int WS_EX_LAYOUTRTL = 0x00400000; public event PropertyChangedEventHandler? PropertyChanged; [DllImport("user32.dll", SetLastError = true)] public static extern bool SetProcessDefaultLayout(uint dwDefaultLayout); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong); } }