wpf Popup usercontrol being chopped off

Wally96333 61 Reputation points
2022-01-24T21:54:22.353+00:00

HI all,

I am developing a file picker for an app.
I have a UserControl for the code to implement the popup.
This is currently in a "toy", with the thought to drop it into the actual app once it is working properly.

Everything seems to be working as it should, but the bottom of the popup is being cut off.
I am at a loss as to how to get the Height property set properly.

Here is the XAML for the popup:

<UserControl x:Class="NS_Select_File_Popup_01_user_control.UserControl_Select_File_01"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:NS_Select_File_Popup_01_user_control"
             mc:Ignorable="d" 
        Loaded="M_EVT_UserControl_Loaded"
        PreviewKeyDown="UserControl_event_Keydown"
        PreviewKeyUp="UserControl_event_Keyup"
        KeyDown="UserControl_event_Keydown"
        KeyUp="UserControl_event_Keyup"
             HorizontalContentAlignment="Stretch"
             VerticalContentAlignment="Stretch"
             BorderBrush="DarkOrange"
             BorderThickness="8"
    d:DesignHeight="990"
    d:DesignWidth="1244"
            Focusable="True"
            Background="DarkRed">

    <UserControl.Resources>
    <Style TargetType="{x:Type Button}" x:Key="Button_select_file_style" >
    <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
    </Style.Triggers>
    </Style>
</UserControl.Resources>

    <Grid Name="GRID_MAIN">
        <Grid.RowDefinitions>
            <RowDefinition Height="44" />
            <RowDefinition Height="720" />
            <RowDefinition Height="80" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1244"/>
        </Grid.ColumnDefinitions>

        <TextBlock Name="TextBlock_file_name"
                   Grid.Row="0"
                   Grid.Column="0"
                   HorizontalAlignment="Left" VerticalAlignment="Top"
                   TextAlignment="Left"
                   Foreground="Black" Background="LightBlue"
                   FontSize="20" FontFamily="Courier New"
                   FontWeight="Bold"
                   Margin="8"
                   Height="34"
                   Width="1000"
                   Padding="4"></TextBlock>
        <Border
            BorderBrush="Yellow" BorderThickness="8"
                   Grid.Row="1"
                   Grid.Column="0"
                Margin="4" Padding="4"
            HorizontalAlignment="Left" VerticalAlignment="Top">

            <ScrollViewer Name="ScrollViewer_selection"
                Height="Auto"
                MaxWidth="1140"
                Background="BlueViolet"
                BorderBrush="Green"
                BorderThickness="8"
                HorizontalScrollBarVisibility="Auto"
                VerticalScrollBarVisibility="Disabled"
                HorizontalAlignment="Left" VerticalAlignment="Top">

            <WrapPanel
                Name="WrapPanel_selection_buttons"
                Orientation="Vertical"
                MaxHeight="672"
                Background="DarkRed"
                Margin="4"
                HorizontalAlignment="Left" VerticalAlignment="Top" >
            </WrapPanel>

        </ScrollViewer>
        </Border>

        <StackPanel Name="StackPanel_full_path"
                   Grid.Row="2"
                   Grid.Column="0"
            Orientation="Vertical"
            Margin="0"
            Width="Auto"
            Height="Auto"
            HorizontalAlignment="Left" VerticalAlignment="Center">
            <TextBlock
                Name="TextBlock_full_path"
                FontSize="20" FontFamily="Courier New"
                FontWeight="Bold"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Height="Auto" Width="1000" Padding="4"
                Foreground="Black" Background="LightBlue"></TextBlock>
                <StackPanel
                    Orientation="Horizontal"
                    HorizontalAlignment="Left" VerticalAlignment="Top"
                    Margin="0,4,0,0"
                    Width="Auto"
                    Height="Auto">
                <Button Name="Button_select" Click="Button_Click_select"
                    Width="488" Padding="8"
                        Height="34"
                    FontSize="16" FontFamily="Courier New"
                       FontWeight="Bold"
                    HorizontalAlignment="Left"
                    BorderBrush="Aquamarine" BorderThickness="4"
                    Foreground="White" Background="DarkGreen">_SELECT</Button>
                <Button Name="Button_close" Click="Button_Click_close"
                    Width="488" Padding="8"
                    Height="34"
                    FontSize="16" FontFamily="Courier New"
                       FontWeight="Bold"
                    HorizontalAlignment="Left"
                    BorderBrush="Aquamarine" BorderThickness="4"
                    Foreground="White" Background="DarkRed">_CLOSE</Button>
                </StackPanel>
            </StackPanel>
    </Grid>
</UserControl>

Here is the code-behind for the popup:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using NS_Select_File_Popup_01;
using NS_File_Selection_LIB_01;

namespace NS_Select_File_Popup_01_user_control
{
    /// <summary>
    /// Interaction logic for UserControl_Select_File_01.xaml
    /// </summary>
    public partial class UserControl_Select_File_01 : UserControl
    {
        public class CLS_file_spec
        {
            private string V__file_name;
            public string GS_file_name
            { get { return V__file_name; }
                set { V__file_name = value; }
            }
            internal CLS_file_spec(string P_name)
            {
                V__file_name = P_name;
            }
        }

        private List<CLS_Button_select_file_01> V__LIST_Buttons_filename;

        private CLS_Button_select_file_01 V__active_filename_button;
        private CLS_Button_select_file_01 V__starting_filename_button;

        private int V__file_count, V__active_index, V__last_index;
        private bool V__is_initializing;
        private Action [] V__ARY_key_handlers;

        private const int V_C_file_column_count = 17;

        public UserControl_Select_File_01()
        {
            InitializeComponent();

            V__file_count = 300;

            M_set_key_handlers();

            V__LIST_Buttons_filename
              = new List<CLS_Button_select_file_01> (V__file_count);

            V__active_index = 1;
            V__is_initializing = true;

            M_load_test_set (WrapPanel_selection_buttons);
            M_set_changed_position();

            V__MainWindow = Application.Current.MainWindow as MainWindow;
            V__is_initializing = false;
        }

        private void M_load_test_set (WrapPanel P_WrapPanel)
        {
            CLS_Button_select_file_01 L_new_filename_button;
            string L_fake_path = "G:\\folder1\\folder2\\folder3\\folder4\\";
            string L_file_name, L_file_spec;

            for (int ndx = 0; ndx < V__file_count; ndx++)
            {
                if (ndx > 100)
                    L_file_name = "File_longer_0100_" + ndx;
                else
                if (ndx > 20)
                    L_file_name = "File_20+_" + ndx;
                else
                    L_file_name = "File_" + ndx;

                L_file_spec = L_fake_path + L_file_name;
                L_new_filename_button
                  = M_new_filename_button (L_file_name, L_file_spec, ndx);

                L_new_filename_button
                  .Style = (Style) Resources["Button_select_file_style"];

                L_new_filename_button.IsEnabledChanged += M_enabled_changed;
                V__active_filename_button = L_new_filename_button;
                L_new_filename_button.M_set_for_disabled ();
                P_WrapPanel.Children.Add (L_new_filename_button);
                V__LIST_Buttons_filename.Add (L_new_filename_button);
            }

            V__starting_filename_button = V__LIST_Buttons_filename[1];
            V__last_index = V__LIST_Buttons_filename.Count - 1;
        }

        private void M_EVT_UserControl_Loaded(object sender,RoutedEventArgs e)
        {
            V__MainWindow = Application.Current.MainWindow as MainWindow;
            V__MainWindow.PreviewKeyDown += UserControl_event_Keydown;
        }

        private MainWindow V__MainWindow;

        private CLS_Button_select_file_01
                M_new_filename_button
                  (string P_name, string P_path, int P_index)
        {
            CLS_Button_select_file_01
            L_Button_select_file_01
              = new CLS_Button_select_file_01 (P_name, P_path, P_index);
            L_Button_select_file_01.Click += M_select_file_Click;

            return L_Button_select_file_01;
        }

        public void M_enabled_changed (object sender, DependencyPropertyChangedEventArgs e)
        {
            if (V__is_initializing)  return;

            if (IsEnabled)
                V__active_filename_button.M_set_for_enabled ();
            else
                V__active_filename_button.M_set_for_disabled ();
        }

        private void UserControl_event_Keydown(object sender,KeyEventArgs e)
        {
            V__ARY_key_handlers[(int) e.Key] ();
            V__active_filename_button.Focus ();
        }

        private void UserControl_event_Keyup(object sender,KeyEventArgs e)
        {
        }

        private void M_set_key_handlers()
        {
            V__ARY_key_handlers = new Action [256];
            for (int ndx = 0; ndx < 256; ndx++)
                V__ARY_key_handlers[ndx] = M_do_nothing;
            V__ARY_key_handlers[(int) Key.Up] = M_handle_uparrow;
            V__ARY_key_handlers[(int) Key.Down] = M_handle_downarrow;
            V__ARY_key_handlers[(int) Key.Left] = M_handle_leftarrow;
            V__ARY_key_handlers[(int) Key.Right] = M_handle_rightarrow;
            V__ARY_key_handlers[(int) Key.Home] = M_handle_home;
            V__ARY_key_handlers[(int) Key.End] = M_handle_end;
            V__ARY_key_handlers[(int) Key.Escape] = M_handle_escape;
        }

        private void M_set_changed_position()
        {
            V__active_filename_button = V__LIST_Buttons_filename[V__active_index];
            V__active_filename_button.M_set_for_enabled ();
            TextBlock_file_name.Text = V__active_filename_button.G_name;
            TextBlock_full_path.Text = V__active_filename_button.G_full_spec;
        }

        private void M_handle_downarrow()
        {
            if ( (V__active_index + 1) > V__last_index) return;
            V__LIST_Buttons_filename[V__active_index].M_set_for_disabled ();
            V__active_index++;
            M_set_changed_position();
        }
        private void M_handle_uparrow()
        {
            if (V__active_index < 1) return;
            V__LIST_Buttons_filename[V__active_index].M_set_for_disabled ();
            V__active_index--;
            M_set_changed_position();
        }
        private void M_handle_leftarrow()
        {
            if (V__active_index < V_C_file_column_count) return;
            V__LIST_Buttons_filename[V__active_index].M_set_for_disabled ();
            V__active_index -= V_C_file_column_count;
            M_set_changed_position();
        }
        private void M_handle_rightarrow()
        {
            if ((V__active_index + V_C_file_column_count) > V__last_index) return;
            V__LIST_Buttons_filename[V__active_index].M_set_for_disabled ();
            V__active_index += V_C_file_column_count;
            M_set_changed_position();
        }
        private void M_handle_home()
        {
            if (V__active_index <= 0) return;
            V__LIST_Buttons_filename[V__active_index].M_set_for_disabled ();
            V__active_index = 0;
            M_set_changed_position();
        }
        private void M_handle_end()
        {
            if (V__active_index >= V__last_index) return;
            V__LIST_Buttons_filename[V__active_index].M_set_for_disabled ();
            V__active_index = V__last_index;
            M_set_changed_position();
        }

        private void M_select_file_Click(object sender, RoutedEventArgs e)
        {
            if ( ! (sender is CLS_Button_select_file_01) ) return;

            CLS_Button_select_file_01
            L_Button_select_file = sender as CLS_Button_select_file_01;

            V__LIST_Buttons_filename[V__active_index].M_set_for_disabled ();
            V__active_index = L_Button_select_file.G_index;
            M_set_changed_position();
        }

        private void M_handle_escape()
        {
            V__MainWindow.G_popup.close_popup();
        }

        private void Button_Click_select(object sender,RoutedEventArgs e)
        {

        }

        private void M_do_nothing()
        {
        }

        private void Button_Click_close(object sender, RoutedEventArgs e)
        {
            V__MainWindow.G_popup.close_popup();
        }
    }
}

Here is the XAML code for the main window

<Window x:Class="NS_Select_File_Popup_01.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:NS_Select_File_Popup_01"
        xmlns:PLIB="clr-namespace:NS_File_Selection_LIB_01;assembly=File_Selection_LIB_01"
        xmlns:UC_select_file_filename="clr-namespace:NS_Select_File_Popup_01_user_control"
        VerticalContentAlignment="Center"  HorizontalContentAlignment="Center"
        mc:Ignorable="d"
        Title="Popup_test" Height="1288" Width="900">

    <Window.Resources>
        <PLIB:CLS_popup_select_file
            x:Key="UserControl_Select_File"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Height="Auto"
            Placement="Center"
            PlacementTarget="{Binding ElementName=StackPanel_main}">
            <UC_select_file_filename:UserControl_Select_File_01
                Width="1200"
                MinHeight="1000"
                MaxHeight="1000"
                HorizontalAlignment="Left" VerticalAlignment="Top"
                VerticalContentAlignment="Top"
                HorizontalContentAlignment="Left"/>
        </PLIB:CLS_popup_select_file>

        <Style x:Key="FocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle
                            Margin="2" SnapsToDevicePixels="true"
                            Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                            StrokeThickness="1" StrokeDashArray="1 2"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
        <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
        <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
        <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
        <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
        <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
        <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
        <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
        <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
        <Style x:Key="Button_Style_TEST_01" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
            <!--<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>-->
            <Setter Property="Background" Value="DarkGreen"/>
            <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
            <!--<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>-->
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="BorderThickness" Value="4"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                SnapsToDevicePixels="true">
                            <ContentPresenter
                                x:Name="contentPresenter"
                                Focusable="False"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsDefaulted" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <!--<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>-->
                                <!--<Setter Property="Background" TargetName="border" Value="DarkRed"/>-->
                                <Setter Property="Background" TargetName="border" Value="Yellow"/>
                                <Setter Property="Background" Value="DarkRed"/>
                                <!--<Setter Property="Foreground" TargetName="Button_Style_TEST_01" Value="Blue"/>-->
                                <!--<Setter Property="Foreground" TargetName="contentPresenter" Value="Blue"/>-->
                                <Setter Property="Foreground" Value="Blue"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                                <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel
        Name="StackPanel_main" Orientation="Vertical" Height="1280" Width="Auto">
        <TextBlock Name="TextBlock_result"
                   Background="DarkGreen" Foreground="White"/>
        <Button Name="Button_select_file" Click="Button_Click_select_file">Select File</Button>
        <Button Name="Button_TEST" Width="224" Margin="24" Style="{DynamicResource Button_Style_TEST_01}">TEST
        </Button>
            <Button Name="Button_close" Click="Button_Click_close"
                    Width="488" Padding="8"
                    Foreground="White" Background="DarkRed">_CLOSE</Button>
    </StackPanel>
</Window>

Here is the code-behind for the main window

using System.Windows;
using System.Windows.Controls.Primitives;

using NS_File_Selection_LIB_01;

namespace NS_Select_File_Popup_01
{


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
    public partial class MainWindow : Window
    {
        private int V__Count = 0;

        private CLS_popup_select_file V__popup;
        public CLS_popup_select_file G_popup
        { get { return V__popup; } }

        public MainWindow()
        {
            InitializeComponent();
            V__popup = Resources["UserControl_Select_File"] as CLS_popup_select_file;
        }

        private void Button_Click_select_file(object sender, RoutedEventArgs e)
        {
            V__popup.Placement = PlacementMode.AbsolutePoint;
            V__popup.HorizontalOffset = 44;
            V__popup.VerticalOffset = 44;
            V__popup.IsEnabled = true;
            V__popup.IsOpen = true;
            V__popup.StaysOpen = false;
            TextBlock_result.Text = "DONE " + ++V__Count;
        }

        private void Button_Click_close(object sender,RoutedEventArgs e)
        {
            Application.Current.MainWindow.Close ();
        }
    }
}

Although it isn't particularly relevant, here is the code from the library:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace NS_File_Selection_LIB_01
{
    public class CLS_Button_select_file_01 : Button
    {
        private string V_RO_name;
        public string G_name { get { return V_RO_name; } }
        private string V_RO_full_spec;
        public string G_full_spec { get { return V_RO_full_spec; } }
        private int V_RO_index;
        public int G_index { get { return V_RO_index; } }
        private TextBlock V__btn_textblock;

        public CLS_Button_select_file_01
                 (string P_name, string P_full_spec, int P_index)
             : base ()
        {
            V_RO_name = P_name;
            V_RO_full_spec = P_full_spec;
            V_RO_index = P_index;
            this.Content = M_new_btn_textblock (P_name);
            this.HorizontalContentAlignment = HorizontalAlignment.Stretch;
        }

        private TextBlock M_new_btn_textblock (string P_name)
        {
            V__btn_textblock = new TextBlock();
            V__btn_textblock.Text = P_name;
            V__btn_textblock.Padding = new Thickness (4);
            V__btn_textblock.Margin = new Thickness (4);
            V__btn_textblock.FontSize = 16;
            V__btn_textblock.Width = Double.NaN;
            V__btn_textblock.TextAlignment = TextAlignment.Left;
            V__btn_textblock.FontFamily = new FontFamily("Courier New");
            V__btn_textblock.Focusable = true;
            return V__btn_textblock;
        }
        //protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e) {  
        //    e.IsInputKey = (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right);  
        //    base.OnPreviewKeyDown(e);  
        //}
        public void M_set_for_focused ()
        { M_set_for_enabled (); }
        public void M_set_for_not_focused ()
        { M_set_for_disabled (); }
        public void M_set_for_enabled ()
        {
            this.Foreground = Brushes.White;
            this.Background = Brushes.DarkGreen;
            V__btn_textblock.Foreground = Brushes.White;
            V__btn_textblock.Background = Brushes.DarkGreen;
        }
        public void M_set_for_disabled ()
        {
            this.Background = Brushes.DarkSlateGray;
            this.Background = Brushes.DarkBlue;
            V__btn_textblock.Foreground = Brushes.White;
            V__btn_textblock.Background = Brushes.DarkBlue;
        }
    }
}

................... and:

using System.Windows.Controls.Primitives;

namespace NS_File_Selection_LIB_01
{
    public partial class CLS_popup_select_file : Popup
    {
        public void close_popup()
        {
            IsEnabled = false;
            IsOpen = false;
        }
    }
}

There is probably a simple solution to this.

Can anyone please show me what I am doing wrong?

(Please excuse the ugly borders and background colors.
It will be diffeent in the final code.)

TIA.

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
768 questions
{count} votes