Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:17:18+00:00 2026-06-07T16:17:18+00:00

I guess it’s a simple Data Binding issue but I can’t make it work.

  • 0

I guess it’s a simple Data Binding issue but I can’t make it work. I have a DataGrid, a ViewModel with an Observable Collection and a class that represents objects in this collection. The DataGrid has two columns: A DataGridTextColumn and a DataGridTemplateColumn.
The binding of the data with the DataGridTextColumn works but the binding with the DataGridTemplateColumn doesn’t. The corresponding column simply remains empty.

Here is the code:

This is my DataGrid:

<DataGrid x:Name="tdg_Memory" Grid.Column="1" Margin="0" Grid.Row="4" VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="MemoryIndexColumn" Header="Index"/>
        <DataGridTemplateColumn x:Name="MemorySolutionColumn" CellTemplate="{DynamicResource MemorySolutionCellTemplate}" Header="Solution"/>
    <DataGrid.Columns>
<DataGrid>

This is the DataTemplate:

<DataTemplate x:Key="MemorySolutionCellTemplate">
    <Grid x:Name="grd_RootGrid" Height="Auto" Width="Auto" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Label x:Name="lbl_ValueCaption" Content="Value:" Margin="0" Grid.Row="0" Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Right"/>
        <Label x:Name="lbl_FitnessCaption" Content="Fitness:" Margin="0" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top"/>
        <Label x:Name="lbl_ValueValue" Content="65665" Grid.Column="1" Height="Auto" Margin="12,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <Label x:Name="lbl_FitnessValue" Content="121212" Grid.Column="1" Margin="12,0,0,0" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid>
</DataTemplate>

The data binding occurs in code behind:

// setting the itemsSource (seems to work)
jobItem.tdg_Memory.ItemsSource = jobModel.Memory;

// binding of the first column (DataGridTextColumn) seems to work
Binding memoryIndexColumnBinding = new Binding();
memoryIndexColumnBinding.Path = new PropertyPath("Index");
jobItem.MemoryIndexColumn.Binding = memoryIndexColumnBinding;

And here is the ViewModel:

// the observable collection that holds the data for the DataGrid
private ThreadSafeObservableCollection<MemoryItemRepresentative> _memory;

    public ThreadSafeObservableCollection<MemoryItemRepresentative> Memory {
        get { return _memory; }
        set {
            _memory = value;

            FirePropertyChanged("Memory");
        }
    }

Here are the objects included in the collection:

public class MemoryItemRepresentative : ViewModelBase {

        private int _index;

        public int Index {
            get { return _index; }
            set {
                _index = value;

                FirePropertyChanged("Index");
            }
        }

        private MPBILSolution _solution;

        public MPBILSolution Solution {
            get { return _solution; }
            set {
                _solution = value;

                FirePropertyChanged("Solution");
            }
        }

        public MPBILPropabilityVector PropabilityVector;

        public MemoryItemRepresentative () {

        }
    }

And the MBILSolution is (but that should be irrelevant for that problem):

public class MPBILSolution : ISolution {

    public int Position { get; set; }
    public BinaryNumber Value { get; set; }
    public double Fitness { get; set; }

}

What I want to do now is to have the DataGrid first column (the DataGridTextColumn) be bound to the Index property of the MemoryItemRepresentative class which works fine.

Furthermore I want to display the contents of the MPBILSolution object (consisting of Properties Position, Value and Fitness) be bound to the second column (the DataGridTemplateColumn). For that reason I have built the DataTemplate that comprise a label for the Value property and a label for the Fitness property.

I have not posted the actual binding code which is not working because my problem seems to be that the corresponding column of the DataGrid always remains empty. At least the static labels inside the DataTemplate (which are used for captioning) should be visible ?

So I am very thankful for any help.
Any further code on request …

Appendix:

Well now the actual binding seems to make problems: In the DataGrid I can now see the DataTemplate inside the column but the values of the MPBILSolution object are missing:

Here is how I do the binding of this object to the DataTemplate:

DataTemplate memorySolutionCellTemplate = (DataTemplate)jobItem.FindResource("MemorySolutionCellTemplate");

        DependencyObject o = memorySolutionCellTemplate.LoadContent();

        Label l = (Label)VisualTreeAssistant.FindFrameworkElement(o, "lbl_FitnessValue");


        Binding fitnessValueBinding = new Binding();
        fitnessValueBinding.Path = new PropertyPath("Fitness");
        fitnessValueBinding.Mode = BindingMode.OneWay;
        l.SetBinding(Label.ContentProperty, fitnessValueBinding);

The object l seems to get the correct Label object so the problem seems to lie in the last four lines of this binding block ?

Please help again 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:17:21+00:00Added an answer on June 7, 2026 at 4:17 pm

    Why DynamicResource? If you can use StaticResource do so, DynamicResource can be quiet about errors i think (possibly check the Output window)…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i guess this is a really simple problem, but i just can't get it
I guess it's a simple question, but how can I replace nil values in
I guess it is impossible, but I will ask it anyway. I have a
I guess there will be a very simple answer to this. But here goes.
Guess we have simple model , e.g. let it be a Person { Name,
I guess this have been both asked and answered before, but I don't really
Guess this is pretty simple, but searching didn't help... I want an ExpandableListView, where
I guess what I am trying to do should be simple, but don’t know
I guess I've seen it somewhere before, but now I can't remember nor find
I guess its probably a simple thing to do - but is there anyway

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.