0
votes

I have a UserControl file that has a textbox that I want to bind to a property in the code behind file, but for some reason I can't get it to bind. Can some one please tell me what I am doing wrong. Thanks in advance.

XAML:

<ContentDialog  Width="200" Height="400" Background="White" Padding="-40,-20" x:Name="addGreetingDialog" PrimaryButtonText="Save" SecondaryButtonText="Cancel" PrimaryButtonClick="addGreetingDialog_PrimaryButtonClick">


    <Grid Margin="-25,0,-25,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
        </Grid.RowDefinitions>

        <Border Grid.Row="0">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF686868" Offset="0"/>
                    <GradientStop Color="#FF515151" Offset="1"/>
                    <GradientStop Color="#FF676767" Offset="0.5"/>
                </LinearGradientBrush>
            </Border.Background>
            <TextBlock HorizontalAlignment="Center" FontSize="24" Foreground="White"  FontFamily="ms-appx:/Assets/Fonts/Roboto-Light.ttf#Roboto"
                       Margin="10">Add Greetings</TextBlock>
        </Border>
        <TextBox Grid.Row="2" PlaceholderText="Greeting" Text="{Binding NewGreeting, Mode=TwoWay}"></TextBox>
    </Grid>
</ContentDialog>

Code Behind:

 private string _newGreeting;
 public string NewGreeting { get { return _newGreeting; } set { _newGreeting = value; } }

 public AddGreeting()
 {

     this.InitializeComponent();
 }

 private async void addGreetingDialog_PrimaryButtonClick(global::Windows.UI.Xaml.Controls.ContentDialog sender, global::Windows.UI.Xaml.Controls.ContentDialogButtonClickEventArgs args)
 {
 }
2
Are you setting the datacontext? - Gabriel Duarte
No. I didn't know I had to. Can you show me what you mean please? @GabrielDuarte - jonjayallday

2 Answers

1
votes

I figured it out. I was missing this line.

DataContext="{Binding RelativeSource={RelativeSource Self}}
0
votes

Just update

     public AddGreeting()
 {

     this.InitializeComponent();
 }

to

 public AddGreeting()
 {

     this.InitializeComponent();
     DataContext = this;
 }

This: DataContext = this; will tell the form to look for the specified property at the AddGreeting class, which i suppose is where the NewGreeting property is located. If not, lets have for example the following class:

    public class YourClass
    {
        private string _newGreeting;
        public string NewGreeting { get { return _newGreeting; } set { _newGreeting = value; } }

        public YourClass()
        {
            NewGreeting = "Test";
        }
    }

With this, you would have to set the DataContext at the form CodeBehind like this: DataContext = new YourClass(); instead of DataContext = this;. That way you're telling the form to look for the property at YourClass class instead of the AddGreeting class.

Fore more information, i suggest you to look at this tutorial.