Fill visible border in StackPanel - wpf

I put 2 borders inside stackpanel and it should be one border is visible at a time .
I like the visible border to fill stackpanel
<StackPanel>
<Border HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="{Binding Title_RoleVisibilty,
Converter={StaticResource WriteRoleVisibilityToVisibilityConverter}}">
<TextBox Text="{Binding Title}" MaxLength="50" />
</Border>
<Border HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="{Binding Title_RoleVisibilty ,
Converter={StaticResource ReadRoleVisibilityToVisibilityConverter}}">
<TextBlock Text="{Binding Title }" />
</Border>
</StackPanel>
I used stackpanel /Dockpanel and both not filled the desired border filled
Please advice

The duplication in the borders is adding unnecessary problems. Limit the situation to only one border. Hence you don't have to focus on the border visibility, with only one border and you can the put in styles(?) maybe to focus on the difference needed for the target parameters instead.
For example to solve the problem I have taken your example and added a new binding on MaxLength size which will adjust as needed; for that appears to be the only change required.
<StackPanel>
<Border HorizontalAlignment="Center"
VerticalAlignment="Center" >
<TextBox Text="{Binding Title}" MaxLength="{Binding MaxSize, Converter=???" />
</Border>
</StackPanel>

Related

TextBlock not wrapping in Grid

I have the following XAML which is meant to show an Image and two TextBlocks on top of eachother beside it:
<StackPanel Orientation="Horizontal" >
<Image Source="{Binding CoverArt}" Height="150" />
<StackPanel Orientation="Vertical">
<StackPanel>
<TextBlock FontSize="30" Text="{Binding Title}" TextWrapping="Wrap" />
</StackPanel>
<Grid Width="auto">
<TextBlock FontSize="22" Text="{Binding Summary}" TextWrapping="Wrap" />
</Grid>
</StackPanel>
</StackPanel>
My problem is getting the text to wrap. I've tried using a Grid and assigning the columns' width but it didn't work. Neither did setting the width values to auto. The only thing that works is hard-coding the width, but I do not want that. Thanks.
A Stackpanel will stretch to the size of its content, so it's not what I would use. Use a grid, as explained in the following posts:
TextBlock TextWrapping not wrapping inside StackPanel
Text in StackPanel doesn't wrap (wp7)
TextBlock inside stackpanel does not wrap text
A quick comparison:
Before with stackpanel
After with one grid (You might want to rearrange the elements a bit)
The code for the last segment:
<pre>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Ellipse Fill="red" Width="150" Height="150" />
<StackPanel Grid.Column="1" Orientation="Vertical">
<StackPanel>
<TextBlock FontSize="30" Text="basdljhba dnaiks d., kasndca casn oiäc cas lkcnaso ca dxjwöbdq wkjöbdqw dkjwqb " TextWrapping="Wrap" />
</StackPanel>
<Grid Width="auto">
<TextBlock FontSize="22" Text="dewdewdewdewdewewd" TextWrapping="Wrap" />
</Grid>
</StackPanel>
</Grid>
Did you try setting the width of the columns to fractions instead? width="2*" That will give you some boundaries without a pixel set size. Some way or another you need to set a constraint for the container. If you have two columns and no size is set they will get 50% each. 2* will make give that column 2/3 of the total column with, see example below.

Vertical alignment of WPF Checkbox content with respect to checkbox

I have a checkbox whose XAML markup is as follows:
<CheckBox HorizontalContentAlignment="Stretch">
<StackPanel Orientation="Vertical">
<TextBox x:Name="editTextBox"
Text="{Binding Path=Caption, Mode=TwoWay}"
TextWrapping="Wrap"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource booleanToVisibilityConverter}}" />
<TextBlock x:Name="itemText"
TextWrapping="Wrap"
Text="{Binding Path=Caption}"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource reverseBooleanToVisibilityConverter}}">
</TextBlock>
</StackPanel>
</CheckBox>
The idea is that I can switch between the TextBlock (display) and TextBox (edit). However, when running the application, the CheckBox visual (the checkable square) is vertically centered. I would like it to be aligned with the top of the TextBlock/TextBox instead.
I've noticed that when I only include a TextBlock (so no StackPanel, no TextBox), the checkbox is in fact aligned with the top of the TextBlock, so I'm assuming I'm missing some setting on the StackPanel?
First of all don't waste your time with VerticalAlignment or VerticalContentAlignment (or even ControlTemplate). They're not going to do what you want or might expect.
As described on MSDN a BulletDecorator (which is the control that CheckBox and RadioButton uses to render a radio/check button) will set the position of the icon automatically. You have no additional control over this:
A Bullet always aligns with the first line of text when the Child
object is a text object. If the Child object is not a text object, the
Bullet aligns to the center of the Child object.
Unless you change the control template (unnecessary) you'll only be able to position the radio/check icon at the top if the content is text.
So if you do something like this it won't look good because you won't be able to move the icon no matter how many VerticalAlignment properties you try to set.
<RadioButton>
<StackPanel>
<TextBlock Text="First line"/>
<TextBlock Text="Something else"/>
</StackPanel>
</RadioButton>
BUT fortunately you can put pretty much anything you want in a TextBlock using InlineUIContainer. The text (or content) in the first line will dictate the position of the icon automatically. If you want something underneath the first line that isn't text, just use <Linebreak/> and then <InlineUIContainer/>
Here's an example that has an oversized TextBox to show more clearly what's happening.
<RadioButton>
<TextBlock VerticalAlignment="Top" TextWrapping="Wrap">
<TextBlock Text="Products with <" VerticalAlignment="Center" Margin="0,0,5,0"/>
<InlineUIContainer BaselineAlignment="Center">
<TextBox FontSize="30" Width="25" Text="10" Margin="0,0,5,0"/>
</InlineUIContainer>
<TextBlock VerticalAlignment="Center" Margin="0,0,5,0">
<Run Text="days" FontWeight="Bold"/>
<Run Text="inventory" />
</TextBlock>
<LineBreak/>
<InlineUIContainer>
<StackPanel>
<CheckBox Content="Include unsold products" />
<CheckBox Content="Include something else" />
</StackPanel>
</InlineUIContainer>
</TextBlock>
</RadioButton>
CheckBox.Content doesn't give you very much control over how things are displayed. Try this instead:
<DockPanel VerticalAlignment="Top"> <!-- can also be center or bottom -->
<CheckBox/>
<Grid>
<TextBox x:Name="editTextBox"
Text="{Binding Path=Caption, Mode=TwoWay}"
TextWrapping="Wrap"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource booleanToVisibilityConverter}}" />
<TextBlock x:Name="itemText"
TextWrapping="Wrap"
Text="{Binding Path=Caption}"
Visibility="{Binding Path=IsBeingEdited, Converter={StaticResource reverseBooleanToVisibilityConverter}}">
</Grid>
</DockPanel>

WPF layout issue with nested StackPanel straying outside parent StackPanel

I have a horizontal StackPanel with an image and another StackPanel. The inner StackPanel is vertical with two TextBlocks both of which have TextWrapping set to Wrap. However when the text gets wide the inner StackPanel increases in width so that the TextBlocks don't wrap. The outer StackPanel is staying with the grid layout space it has been given. How can I make the inner StackPanel stay within the bounds of the outer StackPanel?
Here's the relevant section of XAML:
<StackPanel Name="_imageAndNameStackPanel"
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
Orientation="Horizontal" Margin="12,12,12,0">
<Image Name="_applicationImage" Source="{Binding Path=ImageUri}"
Stretch="Fill" Height="64" Width="64" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="0,0,12,0" />
<StackPanel Name="_nameStackPanel">
<TextBlock Name="_nameTextBlock" Text="{Binding Path=AppName}"
FontSize="24" VerticalAlignment="Top" TextWrapping="Wrap"/>
<TextBlock Name="_subtitleTextBlock" Text="{Binding Path=Subtitle"
FontSize="18" VerticalAlignment="Top" Margin="0,6,0,0"
TextWrapping="Wrap"/>
</StackPanel>
</StackPanel>
You're probably better off with a DockPanel instead of StackPanel.
<StackPanel Name="_imageAndNameStackPanel"
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
Orientation="Horizontal" Margin="12,12,12,0">
<Image Name="_applicationImage" Source="{Binding Path=ImageUri}"
Stretch="Fill" Height="64" Width="64" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="0,0,12,0" />
<DockPanel Name="_nameStackPanel">
<TextBlock Name="_nameTextBlock" Text="{Binding Path=AppName}"
FontSize="24" VerticalAlignment="Top" TextWrapping="Wrap"
DockPanel.Dock="Top" />
<TextBlock Name="_subtitleTextBlock" Text="{Binding Path=Subtitle"
FontSize="18" VerticalAlignment="Top" Margin="0,6,0,0"
TextWrapping="Wrap" DockPanel.Dock="Top"/>
</DockPanel>
</StackPanel>
Lately I'm finding that 2 out of 3 times that I start with StackPanel I end up changing it to DockPanel.
But… are you sure that the outer StackPanel is not expanding beyond the bounds of its grid cell? You might want to make that one a DockPanel as well, with both the Image and the inner DockPanel having DockPanel.Dock="Left".
You could bind your inner StackPanel's width to the Parent StackPanel's width. Something like:
{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Width}
Give your inner stackpanel or textblock's a fixed width.

Width of child control should match width of parent container

I'm pretty new to WPF. I often find my self struggling with getting a bunch of child controls combined width to match a given parent container.. As in the following:
<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}"
ItemsSource="{Binding}"
PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
PreviewMouseMove="BrugereListBox_PreviewMouseMove"
>
<ListBox.ItemTemplate >
<DataTemplate >
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
<StackPanel Orientation="Vertical" IsEnabled="True"
PreviewMouseLeftButtonDown="StackPanel_PreviewMouseLeftButtonDown">
<StackPanel Orientation="Horizontal" >
<Label>Navn</Label>
<TextBox Text="{Binding Name}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Password</Label>
<TextBox Text="{Binding Password}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Email</Label>
<TextBox Text="{Binding Email}"></TextBox>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In this case I'm interested in getting the width of the children of the stackpanel:
<StackPanel Orientation="Horizontal" >
<Label>Navn</Label>
<TextBox Text="{Binding Name}"></TextBox>
</StackPanel>
to match the width of
<ListBox x:Name="BrugereListBox"
Any generic solution to scenarios like this?
turns out to be a dupe of:
wpf border control to span the width of listboxItem
and the answer given there:
This is probably more to do with the
ListBoxItems themselves not taking up
the full width of the ListBox. Add the
HorizontalContentAlignment="Stretch"
attribute to your ListBox and see if
it stretches the individual items to
fill the width.
so change to:
<ListBox x:Name="BrugereListBox" Grid.Column="0" Grid.Row="3" DataContext="{DynamicResource Brugere}"
ItemsSource="{Binding}"
PreviewMouseLeftButtonDown="BrugereListBox_MouseLeftButtonDown"
PreviewMouseMove="BrugereListBox_PreviewMouseMove"
HorizontalContentAlignment="Stretch"
>
but as Kent says, using a Grid would be better in my opinion.
Use the right panel and you'll be fine. StackPanel makes its own decisions about its width/height depending on its orientation and its children. Use a Grid and it will just take up the space it is given - no more, no less.

Change background color of header in WPF expander

I am trying to change the expander background color. It seems so easy but I can't get it to work.
<Expander Name="expOneDay">
<Expander.Header>
<TextBlock Foreground="CadetBlue" Text="Some Text" HorizontalAlignment="Stretch" />
</Expander.Header>
...
</Expander><br/><br/>
Why doesn't HorizontalAlignment="Stretch" help? I am trying to bind the width of Header to the width of Expander but the result is not nice looking.
here you go, this should do the trick.... You should set the width of the header template to the width of the expander.
<Expander Name="expOneDay"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch" Width="Auto">
<Expander.Header >
<Border Background="Bisque">
<TextBlock Foreground="White" Text="Steve"
Width="{Binding ElementName=expOneDay, Path=ActualWidth}"
HorizontalAlignment="Stretch" />
</Border>
</Expander.Header>
</Expander>

Resources