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

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.

Related

WPF Popup placement relative to main window

I have a WPF application that uses one window as main window. Now I want to display various popups centered in the window. This works fine when the popup is placed inside the window. However, I have implemented the popups as user control; meaning that the main window contains a user controls which itself contains the actual popup. So the UI element tree looks like this:
Window --> UserControlPopup --> Popup
The popup inside the user control is declared like this:
<UserControl x:Class="X.Custom_Controls.ErrorPopup"
...
xmlns:local="clr-namespace:X.Custom_Controls"
mc:Ignorable="d" d:DesignHeight="360" d:DesignWidth="500">
<Popup Name="errorPopup" Height="360" Width="500" Placement="Center" StaysOpen="True" PlacementTarget="{Binding ElementName=mainWindow}">
...
</Popup> </UserControl>
The mainWindow element name is the one of my main window, which is declared as follows:
<Window x:Class="X.MainWindow" x:Name="mainWindow" ...>
The problem is that the popup is not placed in the center, but on the left side. So my guess is that the popup cannot resolve the elementName properly (since it's a child of a child of the mainWindow). Does anyone know how I can solve this issue in XAML?
Update: the solution was to use
PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
Try to acces your main window through x:Reference. This one should work:
<MainWindow x:Name="mainWindow">
<UserControl Tag="{x:Reference mainWindow}">
</MainWindow>
<UserControl x:Name="userControl">
<PopUp PlacementTarget="{Binding Path=Tag, ElementName=userControl}"/>
</UserControl>
Such a way you can use your UserControl in another Controls/UserControls and you will not need to modify it, just set the Tag property from outside, if you use {RelativeSource AncestorType=Window} you will need to modify it.
The solution was to use the following as PlacementTarget:
PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType=Window}}"

WPF user control within user control not rendering path

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.

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.

Is there a way to show a splash screen for WPF application?

Dupe: WPF animated splash screen
I would like to show a splash screen for my WPF application.
what I want to do is to show it while I load dictionary from a file (it takes about 5-6 seconds to load). Is there a way to achieve this in WPF? I would appreciate some tutorial, since this is a little bit more complicated then other questions I posted.
A SplashScreen is really just another Window with no border, and it is not resizable (nor can you interact with it in any way). You'd probably want to hide it from the task bar, center it on the screen, etc. Play around with various settings until you get the effect that you want.
Here's a quick one I whipped up in about 5 minutes to prove the theory:
<Window x:Class="MyWhateverApp.MySplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShowInTaskbar="False"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
Background="Transparent"
AllowsTransparency="True"
Title="Sandbox Splash Screen"
SizeToContent="Width"
Topmost="True"
Height="{Binding RelativeSource={RelativeSource Self},
Path=ActualWidth}">
<Border CornerRadius="8" Margin="15">
<Border.Background>
<ImageBrush ImageSource="Resources\sandtexture.jpeg"
Stretch="Fill" />
</Border.Background>
<Border.Effect>
<DropShadowEffect Color="#894F3B"
BlurRadius="10"
Opacity="0.75"
ShadowDepth="15" />
</Border.Effect>
<TextBlock FontSize="40"
FontFamily="Bauhaus 93"
Foreground="White"
Margin="10"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Text="WPF 3.5 Sandbox">
<TextBlock.Effect>
<DropShadowEffect Color="Black" />
</TextBlock.Effect>
</TextBlock>
</Border>
</Window>
Next, modify your App.xaml file to remove the startup window, and instead raise the Startup event:
<Application x:Class="MyWhateverApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
And in the code-behind, handle the Application_Startup event in whatever way you think is best. For example:
Window1 mainWindow = null;
private void Application_Startup(object sender, StartupEventArgs e)
{
MySplashScreen splash = new MySplashScreen();
splash.Show();
mainWindow = new Window1();
mainWindow.Show();
splash.Close();
}
See WPF 3.5 SP1: Splash Screen
Or within VS2010 click on the Solution Explorer do Add -> New Item, select WPF from the list of installed templates and Splash Screen should be on the bottom of the list in the middle.
Note: The splash screen is removed after the constructor and before/when the main window Window_Loaded callback. I moved all of my initialisation into the main window constructor and it works a treat, and is very easy.
Short answer: Add -> New Item -> Splash Screen. It dumps a PNG in the project - just modify this. note that it supports full alpha transparencey so can contain drop-shadows, etc...

Accessing a RenderTransform in a DataTemplate in Silverlight

I've got a bunch of ContentControls with a DataTemplate like so:
<DataTemplate>
<Canvas>
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1.0" ScaleY="1.0"/>
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
</DataTemplate>
...and I want to change their scales dynamically. I'm new to .NET, so please forgive. I tried to use this technique:
http://msdn.microsoft.com/en-us/library/bb613579.aspx
...but DataTemplates don't appear to have FindName in Silverlight. I then tried binding the Scales like so:
<ScaleTransform ScaleX="{Binding Scale}" ScaleY="{Binding Scale}"/>
...but got a XAML error when I ran.
Am I barking up the wrong tree? I figure this must be possible somehow.
Thank you.
Assuming you don't want to animate the scale, simply include a Scale property in your view model. You cannot access an ancestors DataContext from inside a DataTemplate (WPF supports this, however).
Instead of setting the DataContext of your DataTemplate to your entity, create a wrapper class (ViewModel) that also includes a (INotifyPropertyChanged-firing) Scale property. Now your ContentControl can bind to the Scale property of your view model.

Resources