Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<Canvas>
    <wpfUCAllInOne:UC_TextBox Canvas.Left="100" Canvas.Top="100" Width="80" />
</Canvas>

This usercontrol (UC_TextBox) is set 100 to the left of the canvas.
<UserControl x:Class="UC_TextBox"
             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="100" d:DesignWidth="300">



</UserControl>





But how do I set this control 120 to the canvas IN the usercontrol. There is no parent control or something like it.
Posted
Comments
[no name] 16-May-14 10:48am    
Why do you need to do this? It is the responsibility of the container to place the child elements not the other way around.
quator 16-May-14 10:57am    
In the program, the controls are put in place. But I want to to this from the the database
[no name] 16-May-14 12:26pm    
So then you really want to do this from code like in solution 1 not the XAML.

1 solution

Hi Quator,

Just move the control using Canvas static methods, for example on button click:

UserControl1.cs:

C#
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Canvas.SetLeft(this, 120);
    }
}


UserControl.xaml:

XML
<UserControl x:Class="WpfApplication14.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 removed="Aqua">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</UserControl>


Best Regards,

Shai
 
Share this answer
 
v2
Comments
quator 26-May-14 8:28am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900