1

I would like to publish some information in a two-dimension data-grid. First column the name of the attribute, second the current value. Currently I created an array that I attached to the WPF data-grid and filled it upside down with an Attribute = "Username" and a Value = "Waldow". Now I would like to know if there is maybe another way by using a model class where I define every attribute as a string but in the end can display it in the same way, but have a better code. Let`s say this is my model:

public class InformationModel
{
    [Description("Hostname_Description")]
    [Display(Name = "Hostname_Display")]
    public string Hostname { get; set; }

    [Display(Name = "Username_Display")]
    public string Username { get; set; }

...... more values
        
    }

Now I want a Datagrid like this:

Hostname | PC0004
Username | Waldow

but by just specifying the value:

<DataGrid x:Name="datagrid_information" ItemsSource="{Binding Information}" IsReadOnly="True" AutoGenerateColumns="False" Margin="11,10,10,0" HorizontalAlignment="Left" VerticalAlignment="Top" SelectionMode="Single" AlternationCount="1">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Display}" Header="Attribute"/>
                            <DataGridTextColumn Binding="{Binding Description}" Header="Value"/>
                        </DataGrid.Columns>
                    </DataGrid>

The model class will not be extended, meaning the entries will always be the same and no more rows will be added.

Stephan
  • 335
  • 3
  • 12

2 Answers2

1

You should transform your model into a view model that contains a property per column that you want to display in the DataGrid (and bind to an IEnumerable<InformationViewModel>):

public class InformationModel
{
    public string Attribute { get; set; }
    public string Value { get; set; }
}

You can then set the properties based on the attributes using reflection.

There is no way to retrieve the value of the attributes in XAML though. You can only bind to public properties. It's then up to you as a developer to set these properties.

You might set the Attribute property of the sample class above to the string "Hostname", or to the value of the attribute that you can get using reflection. How the value is set doesn't matter as far as the XAML/UI is concerned.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

As suggested I used an own class. So basically in my main class I set a listener on my informationModel and always when a value got changed it will launch the method Model_PropertyChanged. My InformationModel got extended by INotifyPropertyChanged, which calls OnPropertyChanged always when a value got changed:

public MainViewModel() {
// set listener
            informationmodel.PropertyChanged += Model_PropertyChanged;
}

        private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Property changed: " + e.PropertyName);
            // Update object in list
            var obj = Informationdgm.FirstOrDefault(x => x.Attribute == informationmodel.GetType().GetProperty(e.PropertyName).GetCustomAttribute<DisplayAttribute>()?.Name.ToString());
            if (obj != null)
            {
                obj.Value = informationmodel.GetType().GetProperty(e.PropertyName).GetValue(informationmodel, null);

            }

        }
Stephan
  • 335
  • 3
  • 12