1

I have a WPF project binding my datagrid to xml by dataset like follows:

XAML:

<DataGrid Name="dgConfig" AutoGenerateColumns="False" ItemsSource="{Binding ModulesView}" >
    <DataGrid.Columns>
         <DataGridTextColumn Header="ModelNumber" Binding="{Binding ModelNumber}" IsReadOnly="True"  />
         <DataGridTextColumn Header="ParamName" Binding="{Binding ParamName}" IsReadOnly="True"/>
         <DataGridTextColumn Header="ParamValue" Binding="{Binding ParamValues, Mode=TwoWay}" />
         <DataGridTextColumn Header="DefaultValue" Binding="{Binding DefaultValue}" IsReadOnly="True"/>
         <DataGridTextColumn Header="Address" Binding="{Binding Address}" IsReadOnly="True"/>
         <DataGridTextColumn Header="LowHigh" Binding="{Binding LowHigh}" IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>

ViewModel:

    public ICollectionView ModulesView
    {
        get
        {
            if (_ModulesView == null)
                RefreshModule();
            return _ModulesView;
        }
        set
        {
            _ModulesView = value;
            NotifyPropertyChanged();
        }
    }

    private void RefreshModule()
    {
        ModulesView = new ListCollectionView(sdb.GetModules())
        {
            Filter = obj =>
            {
                var modules = (Module)obj;
                return SelectedProduct != null && Product.ModelNumber == SelectedProduct.ModelNumber;
            }
        };
    }

XML:

  <Modules>
     <ModelNumber>1</ModelNumber>
     <ParamName>I00-I07 Logic</ParamName>
     <ParamValue>1</ParamValue>
     <DefaultValue>1</DefaultValue>
     <Address>41301</Address>
     <LowHigh>0</LowHigh>
 </Modules>
 <Modules>
     <ModelNumber>1</ModelNumber>
     <ParamName>I10-I17 Logic</ParamName>
     <ParamValue>10</ParamValue>
     <DefaultValue>10</DefaultValue>
     <Address>41301</Address>
     <LowHigh>1</LowHigh>
 </Modules>

DataSet:

    public ObservableCollection<Module> GetModules()
    {
        DataSet ds = StoreDbDataSet.ReadDataSet();
        ObservableCollection<Module> modules = new ObservableCollection<Module>();
        foreach (DataRow moduleRow in ds.Tables["Modules"].Rows)
        {
            modules.Add(new Module((UInt16)moduleRow["ModelNumber"], moduleRow["ParamName"].ToString(),
                    (UInt16)moduleRow["ParamValue"], (UInt16)moduleRow["DefaultValue"],(UInt16)moduleRow["Address"],
                    (Boolean)moduleRow["LowHigh"]));
        }
        return modules;
    }

Now I am facing the new problem, I have to parse the xml data by Address and LowHigh, for example: the Address 41301 is a ushort type, I have to parse it to 8 bit(LowHigh == 0 means lowbyte LowHigh == 1 means highbyte), I want to show every single bit data to datagrid, I have spent days on this but got no ideas, could you please tell me how to do this? I would appreciate any suggestions.Thanks a lot!

BarryLib
  • 299
  • 1
  • 5
  • 20
  • It seems to me you're already reading the XML through a dataset, so what is the exact problem? Converting a UInt16 to a byte depending on LowHigh? If so the easiest thing is to have another property on the Module class which does the conversion, and bind to that. – Alex Paven Jul 11 '17 at 10:20
  • Forgive my ambiguous expression, there used to be one `ParamValue` binding to dataset in the datagrid before, now I want to have 8 columns of `ParamValue` in the datagrid by converting UInt16 to byte. – BarryLib Jul 11 '17 at 10:33

1 Answers1

0

Ok, first you can convert the UInt16 to a byte by using BitConverter or manually through shifting bits and casting like here.

Then you can convert a byte to individual bits (if that's what you need) with BitArray or again manually through shifting.

If you want 8 columns each showing an individual bit, then add 8 readonly properties to the Module class that do this, outputting an individual bit each, and bind each of them to a column.

If I misunderstood the question let me know and I'll try to clarify further.

Alex Paven
  • 5,539
  • 2
  • 21
  • 35