Example #1
0
            public ListBoxDemoWindow(Screen screen)
                : base(screen)
            {
                Width = 400;

                var stackPanel = new StackPanel(screen)
                {
                    Orientation = Orientation.Vertical,
                    Padding = new Thickness(16),
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Stretch
                };
                Content = stackPanel;

                var title = new TextBlock(screen)
                {
                    Text = "ListBox demo",
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    ForegroundColor = Color.White,
                    BackgroundColor = Color.Black,
                    ShadowOffset = new Vector2(2),
                    TextOutlineWidth = 1,
                    FontStretch = new Vector2(1.5f)
                };
                stackPanel.Children.Add(title);

                var selectedIndexLabel = new TextBlock(screen)
                {
                    Text = "SelectedIndex: -1",
                    Padding = new Thickness(8),
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    ForegroundColor = Color.White,
                    BackgroundColor = Color.Black,
                    TextHorizontalAlignment = HorizontalAlignment.Left
                };
                stackPanel.Children.Add(selectedIndexLabel);

                var listBox = new ListBox(screen)
                {
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    VerticalAlignment = VerticalAlignment.Stretch,
                };
                listBox.SelectionChanged += (s, e) =>
                {
                    selectedIndexLabel.Text = string.Format("SelectedIndex: {0}", listBox.SelectedIndex);
                };
                stackPanel.Children.Add(listBox);

                for (int i = 0; i < 5; i++)
                {
                    var listBoxItem = new ListBoxItem(screen)
                    {
                        HorizontalAlignment = HorizontalAlignment.Stretch,
                        Padding = new Thickness(8),

                        Content = new TextBlock(screen)
                        {
                            Text = string.Format("Item {0:d2}", i),
                            HorizontalAlignment = HorizontalAlignment.Stretch,
                            ForegroundColor = Color.White,
                            BackgroundColor = Color.Black,
                            TextHorizontalAlignment = HorizontalAlignment.Left
                        }
                    };
                    listBox.Items.Add(listBoxItem);
                }

                var switchSelectionModeButton = new Button(screen)
                {
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    Padding = new Thickness(8),

                    Content = new TextBlock(screen)
                    {
                        Text = "Enable the multiple selection mode",
                        ForegroundColor = Color.White,
                        BackgroundColor = Color.Black
                    }
                };
                switchSelectionModeButton.Click += (Control s, ref RoutedEventContext c) =>
                {
                    var textBlock = switchSelectionModeButton.Content as TextBlock;
                    if (listBox.SelectionMode == ListBoxSelectionMode.Single)
                    {
                        listBox.SelectionMode = ListBoxSelectionMode.Multiple;
                        textBlock.Text = "Disable the multiple selection mode";
                    }
                    else
                    {
                        listBox.SelectionMode = ListBoxSelectionMode.Single;
                        textBlock.Text = "Enable the multiple selection mode";
                    }
                };
                stackPanel.Children.Add(switchSelectionModeButton);

                var closeButton = new Button(screen)
                {
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    Padding = new Thickness(8),

                    Content = new TextBlock(screen)
                    {
                        Text = "Close",
                        ForegroundColor = Color.White,
                        BackgroundColor = Color.Black
                    }
                };
                closeButton.Click += (Control s, ref RoutedEventContext c) => Close();
                stackPanel.Children.Add(closeButton);

                closeButton.Focus();
            }
Example #2
0
        /// <summary>
        /// item が ListBoxItem の場合、それをそのまま項目として追加します。
        /// それ以外の場合、
        /// Content プロパティに item を設定した ListBoxItem インスタンスを生成し、
        /// それを項目として追加します。
        /// </summary>
        /// <param name="item"></param>
        protected override void AddItem(Control item)
        {
            var target = item;

            if (!(item is ListBoxItem))
            {
                target = new ListBoxItem(Screen)
                {
                    Content = item
                };
            }

            base.AddItem(target);
        }