0

I make my Textblocks in C# code and i want to bind JSON data to it. At this moment i want to do it like this:

        if (actualStock == true)
        {
            TextBlock TBActualStock = new TextBlock();
            TBActualStock.Text = "Actuele voorraad: ";
            TBActualStock.FontSize = 18;
            STACKActualStockDeliverTime.Children.Insert(1, TBActualStock);

            TextBlock TBBindActualStock = new TextBlock();
            TBBindActualStock.Text = "{Binding ActualStock}"; //this is where it should bind
            TBBindActualStock.FontSize = 18;
            STACKActualStockDeliverTime2.Children.Insert(1, TBBindActualStock);
        }

This is my XAML code:

 DataContext="{Binding Item}"
    d:DataContext="{Binding Groups[0].Items[0]}">
    <Grid.ChildrenTransitions>
        <TransitionCollection>
            <EntranceThemeTransition/>
        </TransitionCollection>
    </Grid.ChildrenTransitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.Background>
        <ImageBrush ImageSource="/Assets/BackGroundGAC.jpg" Stretch="UniformToFill"/>
    </Grid.Background>

    <Grid Grid.Row="1" x:Name="contentRegion">
        <StackPanel Orientation="Horizontal">
                <StackPanel Orientation="Horizontal">
                    <StackPanel x:Name="STACKActualStockDeliverTime">
                        <TextBlock x:Name="HEADERActualStockDeliverTime" FontSize="24" Text="Voorraad en levertijd"></TextBlock>     
                    </StackPanel>

                    <StackPanel  x:Name="STACKActualStockDeliverTime2">
                        <TextBlock x:Name="Headert" FontSize="24" Text=""></TextBlock>


                    </StackPanel>
                </StackPanel>

Now I want add the Json data to the textblock i make in the C# code. i know in XAML i should use {Binding description} //description is a part of my json object so that works But if i do this in C# code it will just set the text to {Binding Description}

Any idea how i can solve this problem?

ps: I need to do it in C# code and not in XAML.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Proliges
  • 371
  • 4
  • 26

2 Answers2

0

If you are binding in code you should not be doing like that, just assign the value

TextBlock.Text Property

    TextBlock TBBindActualStock = new TextBlock();
    TBBindActualStock.Text =  ActualStock ; 

If you want to bind in code, use Binding Operation,

Binding binding = new Binding();
binding.Path = new PropertyPath("ActualStock");
binding.Source = sourceObject; 

BindingOperations.SetBinding(TBBindActualStock, TextBlock.TextProperty, binding);

refered from Binding String Property in Code-Behind TextBlock

In XAML,

    <TextBlock Text="{Binding ActualStock}
Community
  • 1
  • 1
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Well if i make a textblock in XAML and say {Binding ActualStock} it will bind through XAML code, and i don't want that. Should i make a new instance of my object in order to obtain ActualStock? As you can see in the XAML file i uploaded, i bind Items to the datacontext. should i do something similar in C#? – Proliges Apr 08 '14 at 10:27
  • You should make use of INotifyPropertyChanged for your Property – Sajeetharan Apr 08 '14 at 10:29
0

you can use Binding class for it like this

Binding myBinding = new Binding("ActualStock");
myBinding.Source = myDataObject;//here is your data source.
TBBindActualStock.SetBinding(TextBlock.TextProperty, myBinding);
Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25
  • You're in the right direction :) thanks for your help. take a look at the correct answer for the differences. – Proliges Apr 08 '14 at 11:25