Change the source file of User Control - windows-8

My problem is that I have class library where some user control is defined. I have several projects that use this library. In one of these projects I need to change layout of control. Is it possible to redefine Xaml file in Application project to change layout of the user control ?

May I suggest that you are approaching this problem with the wrong solution? First, I acknowledge that what you are wanting is technically possible. In fact, you could generate your XAML at runtime if you wanted - and make it endlessly dynamic. But that's so complicated when the solution could be so simple.
Visual States
A visual state lets you define the layout of a control or group of controls. Then, it let's you define another layout of a control or group of controls. And, then, again. When your control should look one way for Landscape, you change the state. When your control should look one way for Portrait, you change the state. And, when your control should look a totally different way for a certain app that is consuming it, you just switch the state.
This solution gives you the full design-time support provided through the XAML tooling. It is also aligned with the way XAML was intended to be used. And, it's the simplest. I think it is, at least.
Read this: http://blog.jerrynixon.com/2013/11/windows-81-how-to-use-visual-states-in.html

You just need to set the Width and the Height of the root element(layout) of the UserControl to Auto in its Xaml File, then, you can edit its height and width from your application project as you desire.
Here's a thread about how to set the size of the user control in DesignTime, this may clarify things to you as well.
http://lfhck.com/question/157908/wpf-usercontrol-design-time-size

You can achieve that by naming your controls/elements than accessing them from the application project, this example is in WPF but it would be the same in WinRT:
MainWindow.Xaml :
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<wpfApplication1:UserControl1 x:Name="UserControl1"></wpfApplication1:UserControl1>
</Grid>
</Window>
UserControl.Xaml :
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="MainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="button">button in user control</Button>
</Grid>
</UserControl>
MainWindow.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Grid.SetColumn(UserControl1.button,1);
}
}
When you run this example the button will figure in the second collumn of the MainGrid.
Notice that I'm changing the layout from CodeBehind, if you still want to make changes directly from Xaml in the Application Project, you should create Properties
in the UserControl and make some binding stuff.

Related

XAML User Control Base Element Type

In XAML, you can create your own reusable control type as a UserControl object like this:
<UserControl x:Class="MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Button Content="Test"/>
</Grid>
</UserControl>
But you can also just change the base type in that view to be the same type as the content container like this:
<Grid x:Class="MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Content="Test"/>
</Grid>
What is the difference between these two options? In my example, they both derive from FrameworkElement and diverge in inheritance from there. But it seems like they function the same.
I'm sure there's a good reason for one versus the other, but I'm having trouble coming up with a good scenario to justify the distinction. Can anyone enlighten me?
Honestly, the main reason for having different base types is properties.
If you find that a UserControl doesn't have the dependency properties you need - perhaps you'd like it to expand some content from time to time, then you could base it on an Expander rather than going into the code-behind to type 'propdp' and reinvent the wheel, replace the template instead of just stuffing things into the "content" property and bind to the Expander's "IsExpanded" property.
Doing this allows you to blur the line between UserControl and Custom Control by essentially making a copy of the template of an existing control and adding to it; taking advantage of existing properties and adding new ones in code behind if needed.
The UserControl tag itself is just an average base, but if you feel something else is more fitting to what you want the control to be, then use that.
Or, if all you want is a glorified container to lob a bunch of textboxes or something in that will be frequently used, then you needn't change a thing.

Live Background in Windows Store App

In which way do I have to investigate if I want to bring some life into the background of my Windows Store App? I thought about something very simple for the beginning like moving the sun depending on time or generating bubbles every 10 seconds or moving a plane in different heights for demonstration purposes.
Background property is of type Brush - you can try some animation on it.
The other solution would be to set MediaElement as the background of your app:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<MediaElement Source="sampleVideo.wmv" IsLooping="True" />
<Grid>
</Grid>
</Grid>

Change the default page color on a window 8 store application

When I create a new page on a windows 8 store application it has default color which I want to change. If I remove all elements on the page and change the background color it has no effect. I've set the back ground to pink in my example below. How can I make this color take effect? (I've also removed everything out of App.xaml)
<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="DemoWindows8StoreApp.BasicPage3"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DemoWindows8StoreApp"
xmlns:common="using:DemoWindows8StoreApp.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="Pink"
mc:Ignorable="d">
It's best to follow the default templates in that rather than setting the Background of the Page, but of the root element (usually a Grid), I'm not 100% sure why a Background on that page doesn't work (I suspect the control template).
UPDATE 1
According to Control.Background
Each control might apply this property differently based on its visual
template. This property only affects a control whose template uses the
Background property as a parameter. On other controls, this property
has no effect. For more info about visual templates, see the Template
property.
So there might be possible that Page's template doesn't use Background property as a parameter.
Remove nothing from the project to change the color. Go to Common\StandardStyles.xaml. Search for "LayoutRootStyle". You will find style for Panel. Change Background there. Please note this will be affected to all the pages in the project. If you want different color for different page then you can create separate style for each page.
<Style x:Key="LayoutRootStyle" TargetType="Panel">
<Setter Property="Background" Value="Pink"/>
<Setter Property="ChildrenTransitions">
<Setter.Value>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Setter.Value>
</Setter>
</Style>

Background Image of Grid doesn't work

I set a background image to my Gird, and it displays correct in the designer, but when I run the app, the image doesn't appear, the background is black, anyone has any ideas? Below is my code.
<Page.Resources>
<ImageBrush x:Key="BackgroundImage" ImageSource="People/Images/Background.jpg"/>
</Page.Resources>
<Grid Background="{StaticResource BackgroundImage}">
</Grid>
I had the same problem. As Jim suggests, you need to have the correct absolute path.
I originally had:
Assets\Quizzes\image.png
It displayed fine in the designer but not at runtime. I switched it to:
ms-appx:///Assets/Quizzes/image.png
Now it displays correctly in the designer and runtime.

Auto sizing Silverlight controls

I am using Silverlight 4 for a project. I want to know if there is a way I can get the main canvas to stretch to the height and width of the browser window it is hosted in?
I want the controls within to resize in proportion to the main control that hosts all the rest of the controls in the host browser window.
This is a Prism application which has a Shell.xaml and a ContentControl within the shell.
In this prism case I want the content control to span to 100% of the screen height and width.. and when a Page.xaml loads within it I want the Page's usercontrol to fill up the entire content control of the xaml. Similarly, within the Page.xaml if I have a grid I want the grid to grow to a size no more than a fixed number of pixels
The Grid within the Page.xaml's user control seems to be sizing properly. I am having trouble getting the root user control of the Page.xaml to stretch to the entire width of the browser window. Is there a way this can be done using xaml properties only? I dont want to specify the height and width to 800 and 1200 like I have done below.
This is my code
<UserControl x:Class="MyNamespace.MyClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
Background="#FF2D8543"
Height="800" Width="1200"
>
<Grid x:Name="LayoutRoot" Background="#FFEB0A0A"
VerticalAlignment="Center" HorizontalAlignment="Center"
Height="Auto" Width="Auto"
>
</Grid>
Thanks for your time.
Yes, use Grid instead of Canvas as top level element. Grid takes all the available space by default.
Learn more about Silverlight layout system here and here.
Update:
Just remove Width and Height attributes from the UserControl:
Height="800" Width="1200"
By setting those explicitly you are giving the control fixed size. If you don't specify Width and Height the control will take all the available space.