Question

i'm trying to create a simple Windows Store App. I want to reuse some "code" in many pages.

For example i need to reuse someting like this in more than one page..

<StackPanel> 
   <TextBlock Text="Name"/>
   <TextBox x:Name="edtNome"/>

Maybe the best method is using "UserControl"...but i can't realize how!

i've created mine MyUC.xaml

<UserControl
x:Class="Crud.View.MyUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Crud.View"
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">

<Grid>
   <StackPanel>
       <TextBlock Text="Name"/>
       <TextBox x:Name="edtName"/>
   </StackPanel>

But now? I want to put it in my Page.xaml (and in many other), and access the "edtName" from page.xaml code behind.....

what i have to do?

Was it helpful?

Solution

Something like this

in xaml use a binding path. Add an x:Name for your control. ElementName=me 'me' will be the name given to your control

<TextBox x:Name="edtName" Text="{Binding Path=EditName, ElementName=me, Mode=Default}" ..../>

in that code behind add

public string EditName
{
    get { return (string)GetValue(EditNameTextProperty); }
    set { SetValue(EditNameTextProperty, value); }
}

 public static readonly DependencyProperty EditNameTextProperty =
        DependencyProperty.Register("EditName",
            typeof(string),
            typeof(YourClassNameHereForThisControl),
            new FrameworkPropertyMetadata(""));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top