static void Main(string[] args) { //When choosing types for variables that are part of the DOM API, //You will want to use var when it's possible and dynamic when it's not. Console.WriteLine("Starting"); WindowsHost windowsHost = new WindowsHost( ); Window window = new Window( ); Panel panel = new Panel( ) { Margin = new Thickness(1), HorizontalAlignment = HorizontalAlignment.Stretch }; panel.XChildren.Add( new TextBlock( ) { Text = "Press the button", Margin = new Thickness(0, 0, 0, 1), HorizontalAlignment = HorizontalAlignment.Center }); Button button = new Button( ) { Caption = "Button", HorizontalAlignment = HorizontalAlignment.Center }; panel.XChildren.Add(button ); button.OnClick += ( sender, eventArgs ) => { MessageBox.Show( "Info", "Button pressed !", result => { } ); }; window.Content = panel; window.Title = "Hello, Web !"; windowsHost.Show( window ); runWindows( windowsHost ); // ConsoleApplication.Instance.Run( windowsHost ); }
public MessageBox( ) { Panel panel = new Panel(); textBlock = new TextBlock(); textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.VerticalAlignment = VerticalAlignment.Center; textBlock.Margin = new Thickness(1); Button button = new Button( ); button.Margin = new Thickness(4, 0, 4, 0); button.HorizontalAlignment = HorizontalAlignment.Center; button.Caption = "OK"; panel.XChildren.Add( textBlock ); panel.XChildren.Add( button ); panel.HorizontalAlignment = HorizontalAlignment.Center; panel.VerticalAlignment = VerticalAlignment.Bottom; this.Content = panel; }
private static void Main(string[] args) { // Control window = ConsoleApplication.LoadFromXaml( "ConsoleFramework.Layout.xml", null ); //// window.FindChildByName< TextBlock >( "text" ).MouseDown += ( sender, eventArgs ) => { //// window.FindChildByName< TextBlock >( "text" ).Text = "F"; //// eventArgs.Handled = true; //// }; //// window.MouseDown += ( sender, eventArgs ) => { //// window.Width = window.ActualWidth + 3; //// window.Invalidate( ); //// }; // ConsoleApplication.Instance.Run( window ); // return; var assembly = Assembly.GetExecutingAssembly(); var resourceName = "Examples.GridTest.xml"; Window createdFromXaml; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); MyDataContext dataContext = new MyDataContext( ); dataContext.Str = "Введите заголовок"; createdFromXaml = XamlParser.CreateFromXaml<Window>(result, dataContext, new List<string>() { "clr-namespace:Xaml;assembly=Xaml", "clr-namespace:ConsoleFramework.Xaml;assembly=ConsoleFramework", "clr-namespace:ConsoleFramework.Controls;assembly=ConsoleFramework", }); } // ConsoleApplication.Instance.Run(createdFromXaml); // return; using (ConsoleApplication application = ConsoleApplication.Instance) { Panel panel = new Panel(); panel.Name = "panel1"; panel.HorizontalAlignment = HorizontalAlignment.Center; panel.VerticalAlignment = VerticalAlignment.Stretch; panel.XChildren.Add(new TextBlock() { Name = "label1", Text = "Label1", Margin = new Thickness(1,2,1,0) //,Visibility = Visibility.Collapsed }); panel.XChildren.Add(new TextBlock() { Name = "label2", Text = "Label2_____", HorizontalAlignment = HorizontalAlignment.Right }); TextBox textBox = new TextBox() { MaxWidth = 10, Margin = new Thickness(1), HorizontalAlignment = HorizontalAlignment.Center, Size = 15 }; Button button = new Button() { Name = "button1", Caption = "Button!", Margin = new Thickness(1), HorizontalAlignment = HorizontalAlignment.Center }; button.OnClick += (sender, eventArgs) => { Debug.WriteLine("Click"); MessageBox.Show( "Окно сообщения", "Внимание ! Тестовое сообщение", delegate( MessageBoxResult result ) { } ); Control label = panel.FindDirectChildByName("label1"); if (label.Visibility == Visibility.Visible) { label.Visibility = Visibility.Collapsed; } else if (label.Visibility == Visibility.Collapsed) { label.Visibility = Visibility.Hidden; } else { label.Visibility = Visibility.Visible; } label.Invalidate(); }; ComboBox comboBox = new ComboBox( ) { // Width = 14 //HorizontalAlignment = HorizontalAlignment.Stretch }; comboBox.Items.Add( "Сделать одно" ); comboBox.Items.Add("Сделать второе"); comboBox.Items.Add("Ничего не делать"); ListBox listbox = new ListBox( ); listbox.Items.Add( "First item" ); listbox.Items.Add( "second item1!!!!!!1fff" ); listbox.HorizontalAlignment = HorizontalAlignment.Stretch; //listbox.Width = 10; panel.XChildren.Add(comboBox); panel.XChildren.Add(button); panel.XChildren.Add(textBox); panel.XChildren.Add(listbox); //application.Run(panel); WindowsHost windowsHost = new WindowsHost() { Name = "WindowsHost" }; Window window1 = new Window { X = 5, Y = 4, //MinHeight = 100, //MaxWidth = 30, //Width = 10, Height = 20, Name = "Window1", Title = "Window1", Content = panel }; GroupBox groupBox = new GroupBox( ); groupBox.Title = "Группа"; ScrollViewer scrollViewer = new ScrollViewer( ); ListBox listBox = new ListBox( ); listBox.Items.Add( "Длинный элемент" ); listBox.Items.Add("Длинный элемент 2"); listBox.Items.Add("Длинный элемент 3"); listBox.Items.Add("Длинный элемент 4"); listBox.Items.Add("Длинный элемент 5"); listBox.Items.Add("Длинный элемент 6"); listBox.Items.Add("Длинный элемент 700"); listBox.HorizontalAlignment = HorizontalAlignment.Stretch; listBox.VerticalAlignment = VerticalAlignment.Stretch; scrollViewer.Content = listBox; // scrollViewer.HorizontalAlignment = HorizontalAlignment.Stretch; scrollViewer.VerticalAlignment = VerticalAlignment.Stretch; scrollViewer.HorizontalScrollEnabled = false; groupBox.Content = scrollViewer; groupBox.HorizontalAlignment = HorizontalAlignment.Stretch; windowsHost.Show(new Window() { X = 30, Y = 6, //MinHeight = 10, //MinWidth = 10, Name = "LongTitleWindow", Title = "Очень длинное название окна", Content = groupBox }); windowsHost.Show(window1); windowsHost.Show(createdFromXaml); //textBox.SetFocus(); todo : научиться задавать фокусный элемент до добавления в визуальное дерево //application.TerminalSizeChanged += ( sender, eventArgs ) => { // application.CanvasSize = new Size(eventArgs.Width, eventArgs.Height); // application.RootElementRect = new Rect(new Size(eventArgs.Width, eventArgs.Height)); // }; //windowsHost.Width = 80; //windowsHost.Height = 20; application.Run(windowsHost);//, new Size(80, 30), Rect.Empty); } }
public Menu( ) { Panel stackPanel = new Panel( ); stackPanel.Orientation = Orientation.Horizontal; this.AddChild( stackPanel ); // Subscribe to Items change and add to Children them this.items.ListChanged += ( sender, args ) => { switch ( args.Type ) { case ListChangedEventType.ItemsInserted: { for ( int i = 0; i < args.Count; i++ ) { MenuItemBase item = items[ args.Index + i ]; if (item is Separator) throw new InvalidOperationException("Separator cannot be added to root menu."); if (((MenuItem)item).Type == MenuItemType.Submenu) ((MenuItem) item).Type = MenuItemType.RootSubmenu; stackPanel.XChildren.Insert( args.Index + i, item ); } break; } case ListChangedEventType.ItemsRemoved: for (int i = 0; i < args.Count; i++) stackPanel.XChildren.RemoveAt(args.Index); break; case ListChangedEventType.ItemReplaced: { MenuItemBase item = items[ args.Index ]; if (item is Separator) throw new InvalidOperationException("Separator cannot be added to root menu."); if (((MenuItem)item).Type == MenuItemType.Submenu) ((MenuItem)item).Type = MenuItemType.RootSubmenu; stackPanel.XChildren[args.Index] = item; break; } } }; this.IsFocusScope = true; this.AddHandler( KeyDownEvent, new KeyEventHandler(onKeyDown) ); this.AddHandler( PreviewMouseMoveEvent, new MouseEventHandler(onPreviewMouseMove) ); this.AddHandler( PreviewMouseDownEvent, new MouseEventHandler(onPreviewMouseDown) ); }
/// <summary> /// Первая строчка всплывающего окна - особенная. Она прозрачна с точки зрения /// рендеринга полностью. Однако Opacity для событий мыши в ней разные. /// Первые width пикселей в ней - непрозрачные для событий мыши, но при клике на них /// окно закрывается вызовом Close(). Остальные ActualWidth - width пикселей - прозрачные /// для событий мыши, и нажатие мыши в этой области приводит к тому, что окно /// WindowsHost закрывает окно как окно с OutsideClickClosesWindow = True. /// </summary> public Popup( IEnumerable<MenuItemBase> menuItems, bool shadow, int parentItemWidth) { this.parentItemWidth = parentItemWidth; this.shadow = shadow; panel = new Panel(); panel.Orientation = Orientation.Vertical; foreach (MenuItemBase item in menuItems) { panel.XChildren.Add( item ); } Content = panel; // If click on the transparent header, close the popup AddHandler( PreviewMouseDownEvent, new MouseButtonEventHandler(( sender, args ) => { if ( Content != null && !Content.RenderSlotRect.Contains( args.GetPosition( this ) ) ) { Close(); if ( new Rect( new Size( parentItemWidth, 1 ) ).Contains( args.GetPosition( this ) ) ) { args.Handled = true; } } })); EventManager.AddHandler(panel, PreviewMouseMoveEvent, new MouseEventHandler(onPanelMouseMove)); }