3
\$\begingroup\$

I have a class that is a wrapper for memory positions of a hardware device.

I need to data-bind each of this positions, so I created a class with hardcoded properties, one for each position, so that I can bind a WPF DataGrid to it.

Problem is: given the sequential nature of the positions, the code became quite repetitive.

Model(.cs):

public class TabelaMemoria
{

    public string Nome { get; private set; }

    int Numero { get; set; }
    MiotoolWrapper _device;

    public TabelaMemoria(MiotoolWrapper deviceWrapper, string nome, int numero)
    {
        _device = deviceWrapper;
        Nome = nome;
        Numero = numero;
    }

    public float A1 
    {
        get { return getVal(0); } 
    }

    public float B1
    { 
        get { return getVal(1); } 
    }

    public float A2 
    {
        get { return getVal(2); } 
    }

    public float B2
    {
        get { return getVal(3); } 
    }

    public float A3
    {
        get { return getVal(4); } 
    }

    public float B3 
    { 
        get { return getVal(5); }
    }

    public float A4 
    { 
        get { return getVal(6); }
    }

    public float B4
    { 
        get { return getVal(7); }
    }



    private float getVal(int p)
    {
        return _device.GetCalibrationValue(Numero, p);
    }

}

View(.xaml):

<DataGrid
    ItemsSource="{Binding Memorias}"  <!-- of type IEnumerable<TabelaMemoria> -->
    AutoGenerateColumns="False"
    ColumnWidth="Auto">

    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="Content" Value="{Binding Nome}"/>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Header="A1" Binding="{Binding A1}" Width="100"/>
        <DataGridTextColumn Header="B1" Binding="{Binding B1}" Width="100"/>
        <DataGridTextColumn Header="A2" Binding="{Binding A2}" Width="100"/>
        <DataGridTextColumn Header="B2" Binding="{Binding B2}" Width="100"/>
        <DataGridTextColumn Header="A3" Binding="{Binding A3}" Width="100"/>
        <DataGridTextColumn Header="B3" Binding="{Binding B3}" Width="100"/>
        <DataGridTextColumn Header="A4" Binding="{Binding A4}" Width="100"/>
        <DataGridTextColumn Header="B4" Binding="{Binding B4}" Width="100"/>
    </DataGrid.Columns>
</DataGrid>
\$\endgroup\$
1
\$\begingroup\$

I don't have VS on my laptop, but perhaps an enum might make it a bit simpler?

(Heads up: You might need to make a few changes to get this to work)

Model

public class TabelaMemoria {
    public string Nome { get; private set; }

    int Numero { get; set; }
    MiotoolWrapper _device;

    public TabelaMemoria(MiotoolWrapper deviceWrapper, string nome, int numero)
    {
        _device = deviceWrapper;
        Nome = nome;
        Numero = numero;
    }

    public enum Position{
        A1=0,
        B1,
        A2,
        B2,
        A3,
        B3,
        A4,
        B4
    }

    public float getval(Position p){
        return _device.GetCalibrationValue((int)p);
    }
}

View.xaml

<DataGrid.Columns x:Name="grdColumns"/>

View.Xaml.cs

public View{
    public View(){
        InitializeComponent();
        InitializePositions();
    }

    private void InitializePositions(){
        foreach (Position p in Enum.GetValues(typeof(Position))) {
            DataGridTextColumn dgrid = new DataGridTextColumn();
            dgrid.Width = 100;
            dgrid.Header = p.ToString();
            dgrid.Text = (int)p;
        }
    }
}

Now if you add anymore positions, just add it to the enum and it should automatically be picked up. I know it screws up the binding, but I wasn't sure what your specific use case was.

If nothing else, perhaps this will get your gears spinning on a better solution. Good luck, mate!

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.