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.

2 Answers 2

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}
Sign up to request clarification or add additional context in comments.

2 Comments

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#?
You should make use of INotifyPropertyChanged for your Property
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);

1 Comment

You're in the right direction :) thanks for your help. take a look at the correct answer for the differences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.