Reputation: 73
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 ?
Upvotes: 0
Views: 74
Reputation: 31813
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
Upvotes: 1
Reputation: 8299
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.
Upvotes: 0
Reputation: 8299
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
Upvotes: 0