Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to make a window automatically grow with its contents

$
0
0

Hello All,

I have a window, with the windows title bar removed, and in there, I have a custom defined listbox, lst, that binds to code behind. 

The minimum amount of items in this list is one - and hence I would like the size of my window, to only grow big enough to have one list item display nicely. 

Like so:

However, of my list in code behind contains more than one entry, ( lets say 3 entries) I want the listbox and the window to grow bigger and not look like this:

Could you please help me in figuring this out? Sorry I dont think you can copy and paste my xaml as there are too many code dependencies. Here is the xaml anyway:

<Window 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:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:cl="clr-namespace:CommunicationLayer"
    xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="CommunicationLayer.CustomListControl"
    Closing="Window_Closing" ResizeMode="CanResizeWithGrip" Topmost="True"
    AllowsTransparency="True" WindowStyle="None" Background="Transparent"
    SizeToContent="Width"    
    Left="{cl:SettingBinding Left}" Top="{cl:SettingBinding Top}"
    MinHeight="180" MinWidth="200"
    mc:Ignorable="d" x:Name="Annotation"  Title="{Binding DatabaseColumnName}"><Window.Resources><!-- DV Link Colours --><Color x:Key="InputBoxBorderColour">Black</Color><Color x:Key="DVLinkLightBlue">#7f23b7c2</Color><SolidColorBrush x:Key="DVLinkLightBlueBrush" Color="{DynamicResource DVLinkLightBlue}" /><Color x:Key="DVLinkBlue">#FF00C7D5</Color><SolidColorBrush  x:Key="DVLinkBlueBrush" Color="{DynamicResource DVLinkBlue}" /><Color x:Key="DVLinkBlueComboSelectionBG">#00C7D5</Color><SolidColorBrush  x:Key="DVLinkBlueComboSelectionBGBrush" Color="{DynamicResource DVLinkBlueComboSelectionBG}" /><Color x:Key="DVLinkBlueGradientStop">White</Color><Color x:Key="DVLinkBorderGray">Gray</Color><SolidColorBrush  x:Key="DVLinkBorderGrayBrush" Color="{DynamicResource DVLinkBorderGray}" /><Color x:Key="DisabledForegroundColor">Red</Color><Color x:Key="DVLinkDropdownBGColour">LightGray</Color><Color x:Key="BorderMediumColor">#FF888888</Color><Style x:Key="DVLinkListboxStyle"><!-- this overrides the colour that is displayed when a listbox item is selected (default windows blue theme) --><Style.Resources><ImageBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" /><!-- Background of selected item when not focussed --><SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/></Style.Resources></Style><ControlTemplate x:Key="ComboBoxToggleButtonCircle" 
                 TargetType="{x:Type ToggleButton}"><Grid  ><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition Width="20" /></Grid.ColumnDefinitions><Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2" BorderThickness="0.5"   ><Border.BorderBrush><SolidColorBrush Color="DarkGray" /><!--This will change the colour of the combobox's textbox border (not the dropdown's border)--></Border.BorderBrush></Border><Border Grid.Column="0"
                        CornerRadius="0,0,0,0"
                        Margin="0"  ><!--Margin of 0.75 above neccessary to prevent the RHS colour from 'spilling' throug on the left --><Border.Background><SolidColorBrush Color="Transparent"/></Border.Background></Border><Grid Grid.Column="1"><Ellipse Grid.Column="1" Width="16" Height="16" Fill="{DynamicResource DVLinkBlueBrush}"  Stroke="DarkGray" HorizontalAlignment="Center"
                      VerticalAlignment="Center" Margin="0,0,2,0" /><Path x:Name="Arrow"
                      Grid.Column="1"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      Data="M 0 0 L 4 4 L 8 0 Z" Margin="0,0,2,0" ><Path.Fill><SolidColorBrush Color="{DynamicResource DVLinkBorderGray}"/></Path.Fill></Path></Grid></Grid></ControlTemplate><ControlTemplate x:Key="ComboBoxTextBox"
                 TargetType="{x:Type TextBox}"><Border x:Name="PART_ContentHost" Focusable="True" Background="{TemplateBinding Background}"  /></ControlTemplate><Style x:Key="{x:Type ComboBox}"
               TargetType="{x:Type ComboBox}"><Setter Property="Margin" Value="0,10,0,0"/><Setter Property="Width" Value="120"/><Setter Property="Height" Value="24"/><Setter Property="OverridesDefaultStyle" Value="true" /><Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /><Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /><Setter Property="ScrollViewer.CanContentScroll" Value="true" /><Setter Property="MinWidth" Value="120" /><Setter Property="MinHeight" Value="24" /><Setter Property="VerticalContentAlignment" Value="Center"/><Setter Property="Foreground" Value="{DynamicResource DVLinkBlueBrush}"/><!-- * The currently selected text : displayed in the textbox part of combobox *--><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ComboBox}"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup x:Name="CommonStates"><VisualState x:Name="Normal" /><VisualState x:Name="Disabled"><Storyboard><ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox"
                                                Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"><EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledForegroundColor}" /></ColorAnimationUsingKeyFrames></Storyboard></VisualState></VisualStateGroup><VisualStateGroup x:Name="EditStates"><VisualState x:Name="Editable"><Storyboard><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="PART_EditableTextBox"><DiscreteObjectKeyFrame KeyTime="0"  Value="{x:Static Visibility.Visible}" /></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ContentSite"><DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}" /></ObjectAnimationUsingKeyFrames></Storyboard></VisualState><VisualState x:Name="Uneditable" /></VisualStateGroup><VisualStateGroup x:Name="FocusStates"><VisualState x:Name="Focused"/><!-- ripped out the FocusedState Storyboard so it doesnt do anything when focused.--><VisualState x:Name="Unfocused"/></VisualStateGroup></VisualStateManager.VisualStateGroups><ToggleButton x:Name="ToggleButton"
                                          Template="{StaticResource ComboBoxToggleButtonCircle}"
                                          Grid.Column="2"
                                          Focusable="false"
                                          ClickMode="Press"
                                          IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/><ContentPresenter x:Name="ContentSite"
                                              IsHitTestVisible="False"
                                              Content="{TemplateBinding SelectionBoxItem}"
                                              ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                              Margin="5,3,23,3"
                                              VerticalAlignment="Center" 
                                              HorizontalAlignment="Left"></ContentPresenter><TextBox x:Name="PART_EditableTextBox"  
                                     Style="{x:Null}"     
                                     Template="{StaticResource ComboBoxTextBox}"
                                     HorizontalAlignment="Left"
                                     VerticalAlignment="Center"
                                     SelectionBrush="{DynamicResource DVLinkLightBlueBrush}"
                                     VerticalContentAlignment="Center"
                                     Margin="3,3,23,3"
                                     Focusable="True" 
                                     Background="Transparent"
                                     Text="{TemplateBinding Text}"
                                     Visibility="Hidden"
                                     Foreground="{DynamicResource DVLinkBlueBrush}"
                                     IsReadOnly="{TemplateBinding IsReadOnly}"/><!-- ** VerticalAlignment centers the text in the editable part of the combobox ** --><Popup x:Name="Popup"
                                   Placement="Bottom"
                                   IsOpen="{TemplateBinding IsDropDownOpen}"
                                   AllowsTransparency="True"
                                   Focusable="False"
                                   PopupAnimation="Slide"><Grid x:Name="DropDown"
                                      MinWidth="{TemplateBinding ActualWidth}"
                                      MaxHeight="{TemplateBinding MaxDropDownHeight}"><Border x:Name="DropDownBorder"
                                            BorderThickness="1" ><Border.BorderBrush><SolidColorBrush Color="{DynamicResource DVLinkBorderGray}" /></Border.BorderBrush><Border.Background><LinearGradientBrush EndPoint="0,1" StartPoint="0,0"><GradientStop Color="#e0e4e7" Offset="0"/><GradientStop Color="#a0a3a5" Offset="1"/></LinearGradientBrush></Border.Background></Border><ScrollViewer Margin="4,6,4,6"><StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /></ScrollViewer></Grid></Popup></Grid><ControlTemplate.Triggers><Trigger Property="HasItems" Value="false"><Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" /></Trigger><Trigger Property="IsGrouping" Value="true"><Setter Property="ScrollViewer.CanContentScroll" Value="false" /></Trigger><Trigger SourceName="Popup" Property="AllowsTransparency" Value="true"><Setter TargetName="DropDownBorder" Property="CornerRadius" Value="2" /><Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0" /></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsEditable" Value="false" /><Condition Property="SelectedItem" Value="{x:Null}" /></MultiTrigger.Conditions><Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/></MultiTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><Style x:Key="{x:Type ComboBoxItem}"
               TargetType="{x:Type ComboBoxItem}"><Setter Property="Foreground" Value="Black" /><!-- * text displayed in the dropdown is black in colour * --><Style.Resources><SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00C7D5"/><!-- * when the mouse moves over a combobox entry (item), the colour is our own custom blue, instead of windows system default colour 
                note : somehow the colour parameter above does not accept DVLink defined colours
                * --></Style.Resources></Style><ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}" ><Border Background="{TemplateBinding Background}" BorderBrush="{DynamicResource DVLinkBorderGrayBrush}"
                BorderThickness="1" CornerRadius="2" ><ScrollViewer x:Name="PART_ContentHost"/><!-- This line will ensure that the text is displayed --></Border></ControlTemplate></Window.Resources><Border  HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="#3d3d3d" BorderThickness="5" CornerRadius="30" MinHeight="180" Width="200"><Border.Background><LinearGradientBrush EndPoint="0,1" StartPoint="0,0"><GradientStop Color="#e0e4e7" Offset="0"/><GradientStop Color="#a0a3a5" Offset="1"/></LinearGradientBrush></Border.Background><Grid Background="Transparent" ><Grid.RowDefinitions><RowDefinition Height="60*" /><RowDefinition Height="60*" /><RowDefinition Height="60*" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/></Grid.ColumnDefinitions><Image Name="DragImage" ToolTip="Drag to move application." MinHeight="17" HorizontalAlignment="Right" Margin="0,4,20,0"  VerticalAlignment="Top" Width="16" Source="..\images\MinimizeAndMove\move-button.png" MouseLeftButtonDown="DragApplicationAround"/><Label Content="Window Title" FontSize="18" Height="38" Background="#FF00C7D5" Foreground="DarkGray" HorizontalAlignment="Left" Margin="0,27,0,0" x:Name="Name_lbl" VerticalAlignment="Top" Width="337" VerticalContentAlignment="Center" /><ListBox Grid.Row="1"  Style="{StaticResource DVLinkListboxStyle}" x:Name="lst" Background="Transparent" Margin="0,0,0,0" ItemsSource="{Binding}" DataContext="{Binding}" HorizontalContentAlignment="Stretch" BorderBrush="Transparent" Grid.RowSpan="2"><ListBox.ItemTemplate>  <DataTemplate><StackPanel Background="Transparent" Orientation="Vertical" HorizontalAlignment="Center"   Visibility="{Binding SourceVisibility}"  ><TextBlock Name="validationRule" Text="{Binding Path=Validation}" Background="Transparent" Foreground="Yellow" Margin="14,18,0,0"   Visibility="Collapsed"/><Label Name="DatabaseColumnName" Content="{Binding DatabaseColumnName}" Foreground="{DynamicResource DVLinkBlueBrush}" Height="28" Width="150" Background="Transparent" /><ComboBox x:Name="comboBox1"
                                Margin="0,0,0,8"
                                Background="Transparent"
                                Foreground="{DynamicResource DVLinkBlueBrush}"
                                ItemsSource="{Binding Options}"
                                BorderBrush="Transparent"
                                IsEditable="{Binding SourceMode}"
                                SelectedValue="{Binding DefaultMode, Mode=OneWay}"
                                SelectionChanged="ComboSelectionChanged"
                                Visibility="{Binding ComboVisibility}"
                                ToolTip="{Binding ValidationTooltip}"
                                Height="24" Width="150" />       <TextBox x:Name="textBox1"
                                Margin="0,0,0,8"
                                Background="Transparent"
                                Foreground="{DynamicResource DVLinkBlueBrush}"
                                Visibility="{Binding TextboxVisibility}"
                                ToolTip="{Binding ValidationTooltip}" 
                                Text="{Binding DefaultMode, Mode=OneWay}"
                                TextChanged="TextBoxContentChanged"
                                Height="24" Width="147"/><Line X1="0" Y1="0" X2="170" Y2="0" Stroke="AliceBlue" StrokeThickness="0.5" Margin="0,5,0,0" VerticalAlignment="Bottom"/> </StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox><Button Grid.Row="2" Grid.Column="0" Content="Annotate" BorderThickness="0" Height="29" HorizontalAlignment="Center" Margin="0,0,0,0" x:Name="SubmitAnnotation_btn" VerticalAlignment="Bottom" Width="125" Click="SubmitAnnotation_btn_Click"><Button.Background><LinearGradientBrush StartPoint="0,0" EndPoint="0,1"><LinearGradientBrush.GradientStops><GradientStopCollection><GradientStop Color="{DynamicResource DVLinkBlueGradientStop}" /><GradientStop Color="{DynamicResource DVLinkBlue}" Offset="1.0" /></GradientStopCollection></LinearGradientBrush.GradientStops></LinearGradientBrush></Button.Background><Button.Style><Style TargetType="{x:Type Button}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><Border Background="{TemplateBinding Background}" CornerRadius="2"><ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></ControlTemplate></Setter.Value></Setter><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="Tomato"/></Trigger></Style.Triggers></Style></Button.Style></Button></Grid></Border></Window>

Best Regards,

Harriet


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>