Access controls within a Xaml HUB and c #

3

I have a problem trying to create a form within a Hub in a UWP application.

<Hub>
   <HubSection>
     <Datatemplate>
       <Grid>
         <TextBox x:Name="txtSerial"/>
       </Grid>
     </Datatemplate>
   </HubSection>
 </Hub>

The problem is that I can not access the control textbox with the name to get its value. I have read in some pages that you could use the event loaded of the control and a field in the class to make it accessible, but this would complicate me a lot since I want to create several forms with several fields each. If anyone can help me in advance thank you.

    
asked by Edwin V 07.01.2016 в 01:51
source

1 answer

3

Just as you have it at all times you can access the value of the TextBox control by simply doing this from Code behind:

txtSerial.Text

However it is a bad practice, if you are still starting it may be OK, but my recommendation is always to use MVVM and therefore never directly access the control, but let the binding is responsible for linking a property of a class (ViewModel) with a control property.

In summary, I will give you everything you need, or good part. The first thing is this auxiliary class, it will save you lots of work later:

BindableBase

using System.ComponentModel;
public class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public bool SetProperty<T>(ref T propertyBackStore, T newValue, 
                               [CallerMemberName] string propertyName = "")
    {
        if (Equals(propertyBackStore, newValue))
            return false;

        propertyBackStore = newValue;
        if (PropertyChanged != null)
            PropertyChanged(this,
                            new PropertyChangedEventArgs(propertyName)
                            );

        return true;
    }
}

Once you have it you must create a ViewModel, which for the example will suffice:

public class MyViewModel : BindableBase
{
    private string _myStringProperty;
    public string MyStringProperty
    {
        get { return _myStringProperty; }
        set { SetProperty(ref _myStringProperty, value);}
    }
}

Then from your code behind create an instance of this ViewModel, and initialize its values in the Load event like this:

    public MyViewModel ViewModel { get; set; }
    private void pageRoot_Loaded(object sender, RoutedEventArgs e)
    {
        ViewModel = new MyViewModel();
        ViewModel.MyStringProperty = "Asignado por binding";
        //No olvides asignar el ViewModel como DataContext 
        //Tambien se puede hacer por XAML y es más correcto
        //pero vamos de a poco
        DataContext = ViewModel;
    }

Now you have to modify your XAML

<Hub Loaded="pageRoot_Loaded">
   <HubSection>
     <Datatemplate>
       <Grid>
           <TextBox x:Name="txtSerial"  
                    Text="{Binding MyStringProperty}" />
       </Grid>
     </Datatemplate>
   </HubSection>
 </Hub>

That's all, but there's more ... a lot more ... If you want for example that when changing the value of TextBox, automatically change the value of the variable, you can modify the code like this

<TextBox x:Name="txtSerial"  
         Text="{Binding MyStringProperty , Mode=TwoWay}" />

The binding is not as complicated as it seems, it really simplifies your life once you've got used to it.

To make your life easier I recommend you check this article:

Apps, Binding, INotifyPropertyChanged and BindableBase | XAML | C #

And I recommend you this video course:

XAML Tutorial

Especially the chapters dedicated to binding: 5,7,8

    
answered by 07.01.2016 / 03:30
source