Display Control from MainWindow ontop of View - wpf

Is it possible to take a control that is created in the Mainwindow and when switching views... make its zindex topmost to display over top of the view?
I tried playing with ZIndex but had no luck.

ZIndex is a Canvas property, so it won't work for other panel types like Grid, etc. If you need it to appear on top, you could have the control inherit from or placed inside a Popup.
Alternatively, if you add it at the very bottom of the main window's XAML, then it should render on top.

you have to tried DockPanel.
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Top"
Background="OliveDrab"
Foreground="Cornsilk"
FontFamily="Veranda"
FontSize="20"
TextAlignment="Center">
</DockPanel>

Related

ScrollViewer does not display "bar"

I have an ItemsControl wrapped in a ScrollViewer. The ScrollViewer behaves correctly but does not display correctly:
I have no Styling for ScrollViewer anywhere in my app.
The bar size looks like 0px, so the arrows end up touching.
This happens with every ScrollViewer enclosing an ItemsControl in my app.
I tested a simple ItemsControl to remove possibility of other style information effecting it:
<ScrollViewer VerticalScrollBarVisibility="Visible">
<ItemsControl>
<TextBlock Text="test"/>
<TextBlock Text="test"/>
<TextBlock Text="test"/>
</ItemsControl>
</ScrollViewer>
and the result was the same. I also wrapped ItemsControl in a WrapPanel and set Min/Max Heights on the ScrollViewer and WrapPanel and there was no change.
Does anyone know why this is happening and how I can fix it?
Edit:
I looked at the ScrollViewer with snoop and the bizarrely Snoop's ScrollViewers also displayed incorrectly (no track bar):
Thanks

What is the difference between HorizontalAlignment and HorizontalContentAlignment in WPF?

What is the difference between:
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
in a textbox in WPF?
Sample example:
<TextBox HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Height="100"
TextWrapping="Wrap"
AcceptsReturn="True"
></TextBox>
HorizontalAlignment and VerticalAlignment determine the alignment of the control itself with respect to its parent control.
HorizontalContentAlignment and VerticalContentAlignment determine the controls content alignment with respect to the control.
For example consider a common Button control
<Button x:Name="aButton" Width="50" Height="25" />
Here you somehow have to specify how this control is aligned within it's parent control. A suitable parent control could be a StackPanel, a Grid, a WrapPanel etc.
For both Horizontal- and VerticalAlignment you can chose between the options Left, Right, Center and Stretch. The first three options respect the buttons width and height whereas the last option tries to stretch the button into the direction specified ignoring the set width or height:
The code
<StackPanel Orientation="Horizontal">
<Button x:Name="aButton" Width="50" Height="25" HorizontalAlignment="Right" />
</StackPanel>
for example would place the Button inside the StackPanel and align it inside at the left.
HorizontalContentAlignment and VerticalContentAlignment aligns the content of the control. The content is special UIControl that is build into the control which you can simply exploit by taking a look into the ControlTemplate of a ContentControl. Note that we are talking especially about ContenControls which are acting as a container that is capable of taking exactly one object to 'carry' inside and display - its content.
So HorizontalContentAlignment and VerticalContentAlignment are determining the alignment of this content with respect to its container. In the case of a initially created Button the buttons content is its caption and with the two properties in question you are aligning this caption inside the Buttons borders which is again either one of these: Left, Right, Center, Stretch.
HorizontalAlignment will align your textbox in relation to its containing parent whereas HorizontalContentAlignment will align the text of your textbox in relation to itself.
HorizontalContentAlignment and VerticalContentAlignment are used with Content Controls which apply these to its content. For example in the following code the Content Control Button will align its content(which is a string in this case and can be any arbitrary object) to center.
HorizontalAlignment and VerticalAlignment are used with child elements when these are inside a Panel. Panel will arrange its children based on these properties of child elements.
In the following code the Panel (StackPanel) will align its child (Button) to right.
<StackPanel>
<Button Content="OK"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
HorizontalAlignment="Right" VerticalAlignment="Top" />
</StackPanel>

Unable to get vertical scroll bars in an WPF TextBlock

I'm presenting text in a wpf TextBlock control (.Net 3.5). The content of the textblock varies depending on what the user selects in a list box. The text wraps, so I don't need an horizontal scroll bar. However, there is often more text than the amount the window can display, so I need a vertical scroll bar.
As I started searching I quickly found that the answer is to wrap the TextBlock in a ScrollViewer. However, It Does Not Work (TM) and I'm hoping someone can help me work out why.
This is the structure of the UI code:
<Window x:Class=..>
<StackPanel>
<ListBox HorizontalAlignment="Stretch"
VerticalAlignment="Top" Height="200"
SelectionChanged="listbox_changed" SelectionMode="Single">
</ListBox>
<Button Click="Select_clicked">Select</Button>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock Name="textblock" TextWrapping="Wrap"/>
</ScrollViewer>
</StackPanel>
</Window>
When the user selects an item in the list box, some text associated with this item is presented in the TextBlock. I would have thought that the code as it stands should have been all that's required, but it never provides me with a scroll bar.
Searching and experimenting have given me two clues: the root of the problem might be related to me updating the content of the TextBlock dynamically, and that the TextBlock does not resize itself based on the new content. I found a posting that seemed relevant that said that by setting the Height of the TextBlock to its ActualHeight (after having changed its content), it would work. But it didn't (I can see no effect of this).
Second, if I set the height (during design time) of the ScrollViewer, then I do get a vertical scroll bar. For instance, if I set it to 300 in the xaml above, the result is almost good in that the window as first opened contains a TextBlock with a vertical scroll bar when (and only when) I need it. But if I make the window larger (resizing it with the mouse during runtime), the ScrollViewer does not exploit the new window size and instead keeps its height as per the xaml which of course won't do.
Hopefully, I've just overlooked something obvious..
Thanks!
Because your ScrollViewer is in a StackPanel it will be given as much vertical space as it needs to display it's content.
You would need to use a parent panel that restricts the vertical space, like DockPanel or Grid.
<DockPanel>
<ListBox DockPanel.Dock="Top" HorizontalAlignment="Stretch"
VerticalAlignment="Top" Height="200"
SelectionChanged="listbox_changed" SelectionMode="Single">
</ListBox>
<Button DockPanel.Dock="Top" Click="Select_clicked">Select</Button>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBlock Name="textblock" TextWrapping="Wrap"/>
</ScrollViewer>
</DockPanel>

Silverlight Panels

I currently have two panels with controls on my page and when i set the top to hidden and bottom panel to visible the bottom panel is hovering down the middle of the page.
Is there a way to set this / use another control so that the 2nd panel will go to the top of the page.
thanks
<StackPanel>
<local:MyPanel1 Width="300" Height="100" Visibility="Collapsed" />
<local:MyPanel2 Width="300" Height="100" />
</StackPanel>
You can set the panels' visibility in codebehind, or better, databind it to your ViewModel (possibly using a ValueConverter to convert to the Visibility enum).

Silverlight Transparency

Question:
Can a constituent UserControl be transparent in parts for it's external container?
Details:
I have a dialog that I'd like to pull into a separate UserControl. The control contains a Border with a background that has an alpha transparency with a Grid inside it that has a white background and all the content. In xaml markup, it looks something like:
<Border Margin="22,28,20,18" BorderBrush="Black" BorderThickness="1" Background="#30D7D7D7">
<Grid Margin="155,148,145,198" Background="#FFFEFEFE">
<TextBlock Margin="17,19,32,20" Text="Dialog part goes here." TextWrapping="Wrap"/>
</Grid>
</Border>
Problem:
When I move that to a user control, even if I set the user control's background to Transparent, it doesn't display a transparent background for the Border; it's solid.
The UserControl itself will have a background color. Make sure the UserControl you've moved your code to has Background=Transparent.

Resources