I have a border in my WPF-Application and I want to have some text inside of my border like in my picture below.
But I could not figure out how to do it.
My code right now:
<Border Margin="10" BorderThickness="5px" BorderBrush="Gray" CornerRadius="5" Grid.Column="0" Grid.Row="1">
<RadioButton Margin="5">Test</RadioButton>
</Border>
Thanks for your help!
You could use a GroupBox that meets your needs.
<Grid>
<GroupBox Margin="10">
<GroupBox.Header>
Some Text
</GroupBox.Header>
<TextBlock TextWrapping="WrapWithOverflow" Margin="10" Text="Happy every day!"/>
</GroupBox>
</Grid>
I would like to make the "Cartoon" below to be aligned to the right and the yellow part to fill all the space in the middle of my ListBox item.
However, all I can get is this:
Here is my layout xaml:
<Window x:Class="Cartoons.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Cartoons" Height="350" Width="525" SizeToContent="Width">
<DockPanel x:Name="mainPanel">
<Border Background="Green" DockPanel.Dock="Bottom" Width="Auto" Height="Auto">
<Grid Margin="2" Height="Auto">
<ListBox Name="listBoxCartoons" SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Stretch" Background="PowderBlue">
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" DockPanel.Dock="Left" >
<Image Source="<IMAGE_LOCATION>" Width="64" Height="64" Stretch="Fill"/>
</DockPanel>
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="AliceBlue" DockPanel.Dock="Right">
<TextBlock Text="Cartoon" VerticalAlignment="Center"/>
</DockPanel>
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow">
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBlock Text="Character 1"/>
<TextBlock Text="Walt Disney"/>
<TextBlock Text="Speedy Gonzales"/>
</StackPanel>
</DockPanel>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>
</DockPanel>
</Window>
I have tried many things but regardless what I do, it is alwasy displayed with large white space to the right.
Much appreciated,
If you're not avoiding hard-coding numbers in, such as margins, you could try this (tested in Visual Studio for your convenience):
<Border Background="Green" DockPanel.Dock="Bottom" Width="Auto" Height="Auto">
<Grid Margin="2" Height="Auto">
<ListBox Name="listBoxCartoons" SelectionChanged="ListBox_SelectionChanged">
<ListBoxItem>
<!-- Width of the below element may have to be adjusted -->
<Grid HorizontalAlignment="Stretch" Width="499">
<Image Source="<IMAGE_LOCATION>" Width="64" Height="64" Stretch="Fill" HorizontalAlignment="Left"/>
<!-- Margin of the below element may have to be adjusted -->
<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Stretch" Background="Yellow" Margin="69,8,0,8">
<TextBlock Text="Character 1"/>
<TextBlock Text="Walt Disney"/>
<TextBlock Text="Speedy Gonzales"/>
</StackPanel>
<Label Background="AliceBlue" Content="Cartoon" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Right"/>
</Grid>
</ListBoxItem>
</ListBox>
</Grid>
</Border>
Also, there is an extra near the bottom of your XAML, above .
The problem is that the ListBox will generate container elements (of type ListBoxItem) for each item - these will, by default, align content to the left. To change that, add this to your ListBox:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
I figured it out.
Needed to set HorizontalContentAlignment="Stretch" on ListBox.
That resolved the issue. Just modify above line
<ListBox Name="listBoxCartoons" SelectionChanged="ListBox_SelectionChanged">
to
<ListBox Name="listBoxCartoons" HorizontalContentAlignment="Stretch" SelectionChanged="ListBox_SelectionChanged">
and it worked.
How to get these dockpanels right ?
<DockPanel Grid.Row="1" LastChildFill="True" HorizontalAlignment="Stretch">
<DockPanel Width="400" LastChildFill="False" HorizontalAlignment="Left">
<DockPanel>
<TextBlock Width="400" />
</DockPanel>
<DockPanel Height="35" DockPanel.Dock="Bottom" LastChildFill="False">
<Button x:Name="btnRefresh" Content="Refersh" />
</DockPanel>
</DockPanel>
The DockPanel with the TextBlock spans over the DockPanel that is docked at the bottom, I want it to fit right up to it. Any ideas?
Ok, it turns out: the panel docked at the bottom must preceed the dockpanel above it in the xaml declaration. LastChildFill="True" applies to the control that is declared last in the code.
<DockPanel Grid.Row="1" LastChildFill="True" HorizontalAlignment="Stretch">
<DockPanel Width="400" LastChildFill="False" HorizontalAlignment="Left">
<DockPanel Height="35" DockPanel.Dock="Bottom" LastChildFill="False">
<Button x:Name="btnRefresh" Content="Refersh" />
</DockPanel>
<DockPanel>
<TextBlock Width="400" />
</DockPanel>
</DockPanel>
Please refer to the DockPanel Class page at MSDN which has all the help that you need. The XAML example from the linked page:
<DockPanel LastChildFill="True">
<Border Height="25" Background="SkyBlue" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
</Border>
<Border Height="25" Background="Blue" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="White">Dock = "Top"</TextBlock>
</Border>
<Border Height="25" Background="Yellow" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Bottom">
<TextBlock Foreground="Black">Dock = "Bottom"</TextBlock>
</Border>
<Border Width="200" Background="PaleGreen" BorderBrush="Black" BorderThickness="1"
DockPanel.Dock="Left">
<TextBlock Foreground="Black">Dock = "Left"</TextBlock>
</Border>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<TextBlock Foreground="Black">This will fill the remaining space</TextBlock>
</Border>
</DockPanel>
Note the use of the DockPanel.Dock attached properties.
<DockPanel Grid.Row="1" LastChildFill="True" HorizontalAlignment="Stretch">
<DockPanel Width="400" LastChildFill="False" HorizontalAlignment="Left">
<Button x:Name="btnRefresh" Content="Refersh"
Height="35" DockPanel.Dock="Bottom" />
<TextBlock Width="400" />
</DockPanel>
<!-- Other UI Elements here? -->
<DockPanel>
The code below is working nice but the vertical ScrollBar is disabled even if large text inside of TextBlock. How can I enable it?
<UserControl.DataContext>
<viewModels:CommentsViewModel/>
</UserControl.DataContext>
<Grid>
<DockPanel >
<TreeView DockPanel.Dock="Top"/>
<Expander Header="Yo" DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
<ScrollViewer HorizontalScrollBarVisibility="Disabled">
<TextBlock TextWrapping="Wrap" MaxHeight="250"
Text="{Binding Article.Article.Content}"/>
</ScrollViewer>
</Expander>
</DockPanel>
</Grid>
I found.
MaxHeight="250"
tag should be not in the TextBlock, but should be in the Grid to limit height. Then it will be working perfectly.
<UserControl.DataContext>
<viewModels:CommentsViewModel/>
</UserControl.DataContext>
<Grid>
<DockPanel >
<TreeView DockPanel.Dock="Top"/>
<Expander Header="Yo" DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
<Grid MaxHeight="250">
<ScrollViewer HorizontalScrollBarVisibility="Disabled">
<TextBlock TextWrapping="Wrap"
Text="{Binding Article.Article.Content}"/>
</ScrollViewer>
</Grid>
</Expander>
</DockPanel>
</Grid>
If your just displaying text, to be certain it'll work use a TextBox:
<Expander Header="Yo" DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
<Grid IsReadOnly="True">
<TextBox TextWrapping="Wrap" MaxHeight="250"
Text="{Binding Article.Article.Content}"/>
</Grid>
</Expander>
I am trying to modify my simple control so that the text box does not grow as the user types in long text. I took a look at some solutions posted here at Stackoverflow that suggest using a Grid and an invisible Border and binding the Width of the text box to the ActualWidth of the Border, but I can't seem to get it to work in my setup.
Here is the xaml of my control:
<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
<TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
<TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
<StackPanel Orientation="Horizontal">
<Image Source="..\Resources\zoom.png" Width="13"/>
<TextBox Foreground="White" Background="Black" BorderBrush="Transparent">THIS IS SOME TEXT</TextBox>
</StackPanel>
</Border>
</StackPanel>
Any ideas? I need the zoom.png to appear "inside" of the text box so I use a horizontal stack panel and just place the image and text box side by side, both surrounded by the same border.
Is there a way for me to stop my text box from auto growing as text is typed?
Thanks.
UPDATE:
Below is the xaml I am testing with.
<Window x:Class="Desktop.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:composite="http://www.codeplex.com/CompositeWPF"
Title="MyShell" Height="50" Width="900"
WindowStyle="None"
ShowInTaskbar="False"
AllowsTransparency="True"
Background="Transparent"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen">
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="5"
Background="Gray">
<ItemsControl composite:RegionManager.RegionName="MainRegion">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel></WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Border>
</Window>
<UserControl x:Class="Desktop.SearchTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="50" Margin="0">
<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
<TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
<TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
<Grid x:Name="grdTest">
<Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
<TextBox Margin="16,0,0,0" Foreground="White" Background="Black" BorderBrush="Transparent" Width="{Binding ElementName=grdTest, Path=ActualWidth}">THIS IS SOME TEXT</TextBox>
</Grid>
</Border>
</StackPanel>
</UserControl>
I just add my user control to the Window's MainRegion.
I did some more searching and found the solution. I used the below Grid to replace the grid in my original post and now the text box keeps its original size and does not auto grow on long user input. Thanks to all who looked into this.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Source="..\Resources\zoom.png" Width="13"/>
<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" Foreground="White" Background="Black" BorderBrush="Transparent"
Grid.Column="1"
VerticalAlignment="Center"
Text="THIS IS SOME TEXT"/>
</Grid>
Try to put ScrollViewer.HorizontalScrollBarVisibility="Disabled" inside Grid.
Name the Grid as x:Name="grdTest" and try this
<Grid x:Name="grdTest">
<Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
<TextBox Margin="16,0,0,0" Foreground="White" Background="Black" BorderBrush="Transparent"
Width="{Binding ElementName=grdTest, Path=ActualWidth}">THIS IS SOME TEXT</TextBox>
</Grid>
To stop the grow behavior set an explicit size to the TextBox or place it in a grid with the HorizontalAlignment set to stretch
Option 1:
<TextBox Width="60">THIS IS SOME TEXT</TextBox>
Option 2:
<TextBox HorizontalAlignment="Stretch">THIS IS SOME TEXT</TextBox>
I had a very similar issue, and it turned out that my Window property "SizeToContent" was set to "WidthAndHeight", I removed setting that property, (which then defaults to "Manual") and this fixed my similar issue.
To scale the border containing the textbox to the width of the outter stack panel, change the inner stack panel to a grid and set a left margin on the text box so it doesn't overlap with the image. Also set the image's horizontal alignment to left (if that is where you want it) or it will default to the center of the border.
<StackPanel Margin="5,0">
<WrapPanel Margin="0,0,0,5">
<TextBlock Foreground="White" Margin="0,0,2,0">TEXT</TextBlock>
<TextBlock Foreground="#FF0099CC" FontWeight="Bold">MORE TEXT</TextBlock>
</WrapPanel>
<Border Margin="2,4,0,4" BorderThickness="1" SnapsToDevicePixels="True" Background="Black"
BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
<Grid>
<Image HorizontalAlignment="Left" Source="..\Resources\zoom.png" Width="13"/>
<TextBox Margin="16,0,0,0" Foreground="White" Background="Black" BorderBrush="Transparent">THIS IS SOME TEXT</TextBox>
</Grid>
</Border>
</StackPanel>
This article shows a solution for a textbox that is displayed inside a scrollviewer's viewport (in treeview or listbox). A PART_MeasureTextBlock is used (bound to the textbox) to determine the available size and set it to the textbox.
Please have look here and tell me what you think about it:
http://www.codeproject.com/Articles/802385/A-WPF-MVVM-In-Place-Edit-TextBox-Control