Make content inside Button responsive in WPF - wpf

I have vertical navigation bar. Each button of the navigation has it's own image and text. For now the image has static height set to Height="50"and text has the default value. When I resize the window the buttons grow responsive but the content inside keeps the same size.
This is how it look right now:
https://gyazo.com/48cff48437b66f462dccd23639dd59ac (GIF)
My XAML code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<UniformGrid Background="DodgerBlue" Grid.Column="1" Columns="1">
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/home-5-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Home</TextBlock>
</StackPanel>
</Button>
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/search-3-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Search</TextBlock>
</StackPanel>
</Button>
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/twitter-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Twitter</TextBlock>
</StackPanel>
</Button>
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/chat-4-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Chat</TextBlock>
</StackPanel>
</Button>
</UniformGrid>
</Grid>
How could i make content inside the Buttons responsive and grow when the button grows in XAML WPF?

Use Viewbox as shown here.
<Viewbox Stretch="Uniform">
<Button ... />
</Viewbox>

Related

Set the button on top of the video streaming[WPF]

I use LibVLCSharp.WPF in a WPF application.
I want to put some icons and buttons above the video.
But after the VideoView control has loaded, it overwrite everything.
<Canvas Width="325" Height="182">
<Image Source="/Resource/BgLoading_1.png"
Width="325"
Height="182"
Stretch="Fill"
StretchDirection="Both"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Panel.ZIndex="0"
Visibility="Visible" />
<fa:ImageAwesome Foreground="White"
Icon="Spinner"
Spin="True"
Height="48"
Width="48"
Visibility="{Binding LoadImageStatus}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Canvas.Left="139"
Canvas.Top="67" />
<Image x:Name="imgLeftIcon"
Width="30"
Height="30"
Source="{Binding LeftIconSource}"
Stretch="Fill"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,10,10,10"
Visibility="{Binding LeftIconStatus}"
Panel.ZIndex="2" />
<Image x:Name="imgRightIcon"
Width="30"
Height="30"
Source="{Binding RightIconSource}"
Stretch="Fill"
Margin="285,10,10,142"
Visibility="{Binding RightIconStatus}"
Panel.ZIndex="2" />
<!--Video-->
<vlc:VideoView Grid.Row="0"
Height="182"
Width="325"
Visibility="{Binding VideoPlayerStatus}"
Panel.ZIndex="1" />
</Canvas>
RTFM
The controls that must appear on top of the video should be placed as children of the VideoView element.
<Window ...
xmlns:vlc="clr-namespace:LibVLCSharp.WPF;assembly=LibVLCSharp.WPF"
...>
<Grid>
<vlc:VideoView x:Name="VideoView">
<Button x:Name="PlayButton"
Click="PlayButtonOnClick"
Content="Play"
Margin="8"
HorizontalAlignment="Right"
VerticalAlignment="Bottom" />
</vlc:VideoView>
</Grid>
</Window>
Avoid Canvas since they are pretty dumb.
Working example sources based on the manual.
I haven't used vlc, if it covers the icons, I think it should be a window drawn with a separate handle. just like WindowsFormHost. try use Popup control, it can be displayed on top of any control.
<Grid>
<!--video control-->
<Grid Name="video">
<!--Video-->
<vlc:VideoView Grid.Row="0"
Height="182"
Width="325"
Visibility="{Binding VideoPlayerStatus}"
Panel.ZIndex="1" />
</Grid>
<Popup PlacementTarget="{Binding ElementName=video}" Placement="Center">
<Grid>
<Image x:Name="imgLeftIcon"
Width="30"
Height="30"
Source="{Binding LeftIconSource}"
Stretch="Fill"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,10,10,10"
Visibility="{Binding LeftIconStatus}"
Panel.ZIndex="2" />
</Grid>
</Popup>
</Grid>

WPF Canvas as content inside button is not visible if adjacent element fills the space inside a container (DockPanel, Grid)

I have a button like so:
<StackPanel Orientation="Vertical" Width="200">
<DockPanel LastChildFill="False">
<Grid Width="81" Background="Blue" DockPanel.Dock="Right" />
<Button Width="120" DockPanel.Dock="Left">
<Grid>
<TextBlock Text="Button" ></TextBlock>
<Canvas Margin="120,0,0,0" >
<TextBlock Text="I want to break free" />
</Canvas>
</Grid>
</Button>
</DockPanel>
</StackPanel>
In this case the canvas content will not get rendered and it's content won't be visible.
If I however change the Grid lengths from 81 to 80 it works.
It will also work in this scenario:
<Grid Width="200">
<Grid Width="81" Background="Blue" DockPanel.Dock="Right" HorizontalAlignment="Right" Grid.Column="1" />
<Button Width="120" HorizontalAlignment="Left">
<Grid>
<TextBlock Text="Button" ></TextBlock>
<Canvas Margin="120,0,0,0" >
<TextBlock Text="I want to break free" />
</Canvas>
</Grid>
</Button>
</Grid>
The Canvas will also work if not inside a button:
<StackPanel Orientation="Vertical" Width="200">
<DockPanel LastChildFill="False" Height="42">
<Grid Width="81" Background="Blue" DockPanel.Dock="Right" />
<Canvas Margin="120,0,0,0" >
<TextBlock Text="I want to break free" />
</Canvas>
</DockPanel>
</StackPanel>
Why is it behaving like that inside a button? How can be this fixed to the wanted behavior of showing the canvas?

How to avoid overlapping of content in dockpanel

I have two stackpanel inside Dock Panel. When the window is resizing the top panel is overlapping the bottom one.
<Grid>
<DockPanel DockPanel.Dock="Left" LastChildFill="True" Width="{Binding ActualWidth, ElementName=characterLength}">
<!-- Batch information panel: Top -->
<ScrollViewer DockPanel.Dock="Top" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Margin="6,0,0,0">
<StackPanel Orientation="Vertical" Margin="6,0,0,6" >
<TextBlock x:Name="ReadOnlyNoBatchesTextBlock"
DockPanel.Dock="Top"
Text="no batch"
FontSize="19" HorizontalAlignment="left" AutomationProperties.AutomationId="DipIn_BatchReviewView_NoBatches_Text"
FontWeight="Bold"
/>
<TextBlock HorizontalAlignment="Left" FontSize="19" FontWeight="Bold" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchNumber_Text" />
<TextBlock Text="status" HorizontalAlignment="Left" Margin="0,6,0,0" FontSize="16" FontWeight="Bold"
AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchSubmissionStatus_Text">
</TextBlock>
<TextBlock Text="submitted" FontSize="14" HorizontalAlignment="Left"
AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchSubmittedAt_Text">
</TextBlock>
<TextBlock Text="qeqweq" FontSize="14" HorizontalAlignment="Left" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchSubmittedBy_Text" />
<TextBlock Text="ertert" FontSize="16" HorizontalAlignment="Left" Margin="0,6,0,0" FontWeight="Bold" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchProgressed_Text">
</TextBlock>
<TextBlock Text="dsdfgfdg" FontSize="14" HorizontalAlignment="Left" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchProgressedAt_Text" >
</TextBlock>
<!-- Must be last item in the stack panel otherwise you get a vertical gap-->
<TextBlock Grid.Column="0" x:Name="characterLength" Text="abcdefg" HorizontalAlignment="Left" Visibility="Hidden" FontWeight="Bold" FontSize="14" />
</StackPanel>
</ScrollViewer>
<!-- Button panel: Bottom -->
<StackPanel Orientation="Vertical" VerticalAlignment="Bottom">
<!-- Content is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_SetMarkerStandardised_Button" />
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,3,0,3"
AutomationProperties.AutomationId="DipIn_BatchReviewView_AllocateNextStandardisationBatch_Button" />
<!-- Content, Automation Id & Command is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" />
<!-- Content is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,3,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_AutoProgressBatches_Button" />
<!-- Content is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchListFilter_Button" />
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_SubmitBatch_Button" />
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,3,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_StartStop_Marker_Button">
<TextBlock FontSize="14" Text="a" />
</Button>
</StackPanel>
</DockPanel>
</Grid>
I need that when window is resizing bottom panel should always be visible and top panel will get scroll bar.
I have tried many ways but still getting the same issue from this code the top one will get the scroll bar but only when the bottom one will go invisible
<Grid>
<DockPanel>
<!--<ScrollViewer DockPanel.Dock="Top" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Margin="6,0,0,0">-->
<StackPanel Orientation="Vertical" Margin="6,0,0,6" >
<TextBlock x:Name="ReadOnlyNoBatchesTextBlock"
DockPanel.Dock="Top"
Text="no batch"
FontSize="19" HorizontalAlignment="left" AutomationProperties.AutomationId="DipIn_BatchReviewView_NoBatches_Text"
FontWeight="Bold"
/>
<TextBlock HorizontalAlignment="Left" FontSize="19" FontWeight="Bold" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchNumber_Text" />
<TextBlock Text="status" HorizontalAlignment="Left" Margin="0,6,0,0" FontSize="16" FontWeight="Bold"
AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchSubmissionStatus_Text">
</TextBlock>
<TextBlock Text="submitted" FontSize="14" HorizontalAlignment="Left"
AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchSubmittedAt_Text">
</TextBlock>
<TextBlock Text="qeqweq" FontSize="14" HorizontalAlignment="Left" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchSubmittedBy_Text" />
<TextBlock Text="ertert" FontSize="16" HorizontalAlignment="Left" Margin="0,6,0,0" FontWeight="Bold" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchProgressed_Text">
</TextBlock>
<TextBlock Text="dsdfgfdg" FontSize="14" HorizontalAlignment="Left" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchProgressedAt_Text" >
</TextBlock>
<!-- Must be last item in the stack panel otherwise you get a vertical gap-->
<TextBlock Grid.Column="0" x:Name="characterLength" Text="abcdefg" HorizontalAlignment="Left" Visibility="Hidden" FontWeight="Bold" FontSize="14" />
</StackPanel>
<!--</ScrollViewer>-->
<!-- Button panel: Bottom -->
<StackPanel Orientation="Vertical" VerticalAlignment="Bottom">
<!-- Content is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_SetMarkerStandardised_Button" />
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,3,0,3"
AutomationProperties.AutomationId="DipIn_BatchReviewView_AllocateNextStandardisationBatch_Button" />
<!-- Content, Automation Id & Command is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" />
<!-- Content is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,3,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_AutoProgressBatches_Button" />
<!-- Content is set by a Style Trigger -->
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_BatchListFilter_Button" />
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,0,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_SubmitBatch_Button" />
<Button Height="40" HorizontalAlignment="Stretch" Margin="0,3,0,3" AutomationProperties.AutomationId="DipIn_BatchReviewView_StartStop_Marker_Button">
<TextBlock FontSize="14" Text="a" />
</Button>
</StackPanel>
</DockPanel>
</Grid>
Avoid using ScrollViewer it will Avoid the Overlapping of the Content .
You could use a Grid with two RowDefinitions:
<DockPanel DockPanel.Dock="Left" LastChildFill="True" Width="{Binding ActualWidth, ElementName=characterLength}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Margin="6,0,0,0">
<StackPanel Orientation="Vertical" Margin="6,0,0,6" >
...
</StackPanel>
</ScrollViewer>
<StackPanel Grid.Row="1" Orientation="Vertical" VerticalAlignment="Bottom">
...
</StackPanel>
</Grid>
</DockPanel>

Designing a Hamburger menu in WPF with Mahapps.Metro

Currently, I'm using a Grid of Buttons for housing keys for navigation. It's hideous, but gets the job done.
Here's the XAML chunk:
<Button x:Name="ShowMainMenu" Grid.Column="0" Margin="5" Content="Main Menu"/>
<Button x:Name="ShowReportsMenu" Grid.Column="2" Margin="5" Content="Reports"/>
<Button x:Name="Reset" Grid.Column="4" Margin="5" Content="Reset"/>
<Button x:Name="TryClose" Grid.Column="6" Margin="5" Content="Close"/>
Incidentally, I'm using Caliburn.micro, and so naming the buttons is enough to bind them to proper commands.
Now, I want them to put them in a Hamburger Menu.
What I have tried till now is this:
<Controls:HamburgerMenu >
<Controls:HamburgerMenu.ItemTemplate>
<DataTemplate DataType="{x:Type Controls:HamburgerMenuGlyphItem}">
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="12" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Glyph}" />
<TextBlock Grid.Column="1" VerticalAlignment="Center" FontSize="16" Text="{Binding Label}" />
</Grid>
</DataTemplate>
</Controls:HamburgerMenu.ItemTemplate>
<Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuGlyphItem Glyph="1" Label="Main Menu" cal:Message.Attach="[Event Click] = [Action ShowMainMenu]"/>
</Controls:HamburgerMenuItemCollection>
</Controls:HamburgerMenu.ItemsSource>
</Controls:HamburgerMenu>
But it doesn't work, because Controls:HamburgerMenuGlyphItem isn't a FrameworkElement.
By the way, the transitioning view is implemented like this:
So, how can I translate my bunch of Buttons into a HamburgerMenu? I can't find proper documentation for Caliburn.micro's HamburgerMenu as well.
Well, I figured it out myself.
Here's the code:
<DockPanel>
<DockPanel.Resources>
<DataTemplate x:Key="MenuItemTemplate">
<Grid Height="48" Background="Black">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<cal:ActionMessage MethodName="{Binding Tag}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe MDL2 Assets" Foreground="BlanchedAlmond" Text="{Binding Glyph}"/>
<TextBlock Grid.Column="1" VerticalAlignment="Center" FontSize="16" Foreground="White" Text="{Binding Label}"/>
</Grid>
</DataTemplate>
</DockPanel.Resources>
<Controls:HamburgerMenu Foreground="White" PaneBackground="#FF444444" IsPaneOpen="False" DisplayMode="CompactOverlay" OptionsItemTemplate="{StaticResource MenuItemTemplate}" ItemTemplate="{StaticResource MenuItemTemplate}">
<Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuGlyphItem Glyph="M" Label="Main Menu" Tag="ShowMainMenu"/>
<Controls:HamburgerMenuGlyphItem Glyph="I" Label="Invoice" Tag="OpenInvoiceMaker"/>
<Controls:HamburgerMenuGlyphItem Glyph="R" Label="Reports" Tag="ShowReportsMenu"/>
</Controls:HamburgerMenuItemCollection>
</Controls:HamburgerMenu.ItemsSource>
<Controls:HamburgerMenu.OptionsItemsSource>
<Controls:HamburgerMenuItemCollection>
<Controls:HamburgerMenuGlyphItem Glyph="A" Label="About" Tag="About"/>
</Controls:HamburgerMenuItemCollection>
</Controls:HamburgerMenu.OptionsItemsSource>
<Controls:HamburgerMenu.Content>
<DockPanel>
<Border Background="#FF444444" DockPanel.Dock="Top">
<TextBlock x:Name="Header" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Foreground="White" Text="{Binding ActiveItem.DisplayName}" />
</Border>
<ScrollViewer Grid.Row="1" CanContentScroll="True" IsDeferredScrollingEnabled="False">
<Controls:TransitioningContentControl Transition="LeftReplace" x:Name="ActiveItem" Content="{Binding ActiveItem}"/>
</ScrollViewer>
</DockPanel>
</Controls:HamburgerMenu.Content>
</Controls:HamburgerMenu>
</DockPanel>
Basically, I used EventTrigger to achieve the same functionality as a button. The rest was easy.

WP7 Max width textbox and position to set from right

I have this code:
<controls:PivotItem Header="Hledat">
<Canvas Margin="0">
<TextBox Name="SearchTxb" Height="72" Text="" VerticalAlignment="Top" HorizontalAlignment="Right" />
<Button Name="SearchBtn" Margin="0,74,100,0" Width="200" Height="72" Click="SearchBtn_Click" HorizontalAlignment="Right" VerticalAlignment="Top">Hledat</Button>
</Canvas>
</controls:PivotItem>
and I want to support Portrait and Landscape. This should be pivotitem for search. I want to textbox to change width to full size of phone screen and I want to button set position to f.e. 20 from right side. What should I add to code? Thanks
<controls:PivotItem Header="Hledat">
<Grid Margin="0">
<Grid.ColumnDefinition>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinition>
<TextBox Grid.Column="0" Name="SearchTxb" Height="72" Text="" VerticalAlignment="Top" HorizontalAlignment="Stretch" />
<Button Grid.Column="1" Name="SearchBtn" Margin="0,74,100,0" Width="200" Height="72" Click="SearchBtn_Click" HorizontalAlignment="Right" VerticalAlignment="Top">Hledat</Button>
</Grid>
</controls:PivotItem>

Resources