WPF user control within user control not rendering path - wpf

I have a toolbox which is a user control, which has a grid control inside it. Each grid cell is populated by another set of user controls representing each tool. Now each tool has a path with gradient fill representing an icon. The tool user control looks fine in the designer, but when I load the tool into the toolbox nothing shows up. I tried changing the background of the tool and it is reflected in the toolbox. So for some reason the path with the gradient will is not rendered when loaded within another control. Any ideas why?
toolbar
<usercontrol ...>
<local:SettingsButton/>
</usercontrol>
toolbox (SettingsButton)
<usercontrol ...>
<Grid x:Name="LayoutRoot" Background="AliceBlue">
<Path Data="M152.76824,152 ... />
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop.../>
</LinearGradientBrush>
</Path.Fill>
</Grid>
</usercontrol>

Put the control inside a viewbox or else the path wont re-size itself. This helped me.

Related

Change the MahApps Metro Split ButtoniIcon size

I'm trying to use the split button control described here:
http://mahapps.com/controls/split_dropdownbutton.html
I'm also using the MahApps resource pack with the icons collection. I want to re-size the control to a height of 40px, but the icon stays to its native height of 76px, resulting in it getting cropped, as shown below.
Any ideas how to get around this?
The SplitButton from MahApps.Metro has a property called IconTemplate. To this property you can assign a DataTemplate and there you can add almost arbitrary content, which will be put in place of the icon itself.
For example you could put a Rectangle control with fixed Height and/or Width. And setting its OpacityMask to your desired icon, will show the icon in smaller size.
In XAML it looks like this:
<controls:SplitButton Orientation="Horizontal">
<controls:SplitButton.IconTemplate>
<DataTemplate>
<Rectangle VerticalAlignment="Center" Height="35" Width="35" Fill="{DynamicResource BlackColorBrush}">
<Rectangle.OpacityMask>
<VisualBrush Visual="{StaticResource appbar_globe}" />
</Rectangle.OpacityMask>
</Rectangle>
</DataTemplate>
</controls:SplitButton.IconTemplate></controls:SplitButton>
controls is the alias for the MahApps.Metro-namespace and may differ in your project.

ScreenShot as Button content

I want to do something similar to Microsoft Powerpoint: I want to capture the current screen (or one grid from the current window), and then to set it as the content of the button (like powerpoint where there is a preview of the slides at the side bar).
What is the best way to do that? My current idea is to save it to PNG and then reload it as an image at the content of the button, but I'm sure there is a better way.
Thank you!
Try this:
<Grid>
<Viewbox
x:Name="Viewer"
Stretch="Uniform"
DataContext="{Binding ElementName=Thumbnail1, Path=Data}">
<Grid Width="224" Height="168" SnapsToDevicePixels="True" Margin="4,0,4,0">
<Grid.Background>
<VisualBrush Stretch="Uniform" TileMode="None" Visual="{Binding}" AlignmentY="Center"/>
</Grid.Background>
</Grid>
</Viewbox>
</Grid>
I was using saving as PNG approach in the beginning. Then I have found out the way as above. DataContext of the ViewBox is a canvas. I could create live previews of my screens this way however my screens were canvas objects.

Why won't my WPF controls scale when my window is maximized via touching the top of the screen in Windows 7?

I have a borderless window created for my WPF application. It resizes perfectly on the sides and corners and also when I drag it to the left or right side of the screen it perfectly scales to fit half the screen. If I use a button or other control event to maximize the window it works perfectly. However, when I drag the window to the top of the screen, the actual window maximizes but the grid inside it does not.
I have it set up to call a method on the sizeChanged event that sets the size of the grid relative to the window (it fills the entire window except 10 pixels on each edge). At first, I thought the sizeChanged event wasn't firing so I created a thread to just detect if the windowState was maximized. On detection it would simply run the method to size up the grid. The method ran, but the grid size didn't change. Only this method of maximization is a problem.
How do I fix this problem?
Edit: XAML
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF868686" Offset="0" />
<GradientStop Color="Black" Offset="1" />
</LinearGradientBrush>
</Window.Background>
<Grid Background="{x:Null}" Name="baseGrid">
<Grid Height="342" HorizontalAlignment="Stretch" Name="grid1" VerticalAlignment="Stretch" Width="718" Background="#46BA0000">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="6,6,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="maximize" />
</Grid>
</Grid>
I'm also using a style and a theme in my application but this problem happens even when they aren't involved so i didnt include them in the XAML
One solution you can try is to put VerticalAlignment="Stretch" and HorizontalAlignment="Stretch" on your grid control
Other solution is to use e.NewSize.Width and e.NewSize.Height to calculate the grid height and width on Window's SizeChanged event handler.

How to put TextBox on top of Camera for Windows Phone

This is the camera screen using the Rectangle for Augmented Reality:
<Grid x:Name=”LayoutRoot”>
<Rectangle Width=”640″ Height=”480″>
<Rectangle.Fill>
<VideoBrush x:Name=”ViewFinderBrush” />
</Rectangle.Fill>
</Rectangle>
</Grid>
Questions:
1) How do I place a textbox ontop of the Rectangle?
2) How to place map, pin, image ontop of the rectangle dynamically?
Any link of reference for placing object ontop of the Rectangle such as place a Pin for current Gps location?
Thanks
In XAML, you need to add the TextBox control after the Rectangle declaration in order for it to be in front:
<Grid>
<Rectangle Width="640" Height="480">
<Rectangle.Fill>
<VideoBrush x:Name=”ViewFinderBrush” />
</Rectangle.Fill>
</Rectangle>
<TextBox Height="80" Margin="10,10,0,0"></TextBox>
</Grid>
When placing objects dynamically to the Grid, you add them as children and those will be automatically placed on top of the existing layout. You simply need to make sure that you are setting the proper margins.
For example, if you would need to add another TextBox, you could do this:
TextBox t = new TextBox();
t.Height = 80;
mainRoot.Children.Add(t);
Where mainRoot is the name of the host grid. The same applies to other controls that support the ability to set child controls.

WPF Binding a visual brush's visual to a different window

I need a rectangle in my settings window to display a scaled down version of of the main window. This is the non-working code that I have right now. Is it possible to do what I want to do?
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding ElementName=local:MainWindow}" />
</Rectangle.Fill>
Yes, but not in pure XAML and not using ElementName. Instead, you'll need to pass a reference to the main window into your settings window. You can then bind the VisualBrush.Visual to that reference.
As a simplified example, when creating your settings window, you could set its DataContext to the main window:
// MainWindow.xaml.cs
SettingsWindow w = new SettingsWindow { DataContext = this };
w.Show();
Then the SettingsWindow you could access the MainWindow as {Binding} (because the MainWindow is now the SettingsWindow's DataContext, and {Binding} refers to the DataContext):
<!-- SettingsWindow.xaml -->
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding}" />
</Rectangle.Fill>
In practice you probably won't want to pass the main window object as the DataContext because that's too blunt an instrument, but hopefully this gives you the idea.

Resources