0

This XML is deserialized properly from a website and returned as a class object. For example, I can call balanceResponse.accountId in my method and it will display a value, so I know it is working. I use the same process for other web requests and have not had an issue binding them to a WPF DataGrid.

What am I doing wrong in the XAML? It is not displaying any values.

The XML class was generated with special copy paste in Visual Studio by the way.

XAML:

       <DataGrid x:Name="accountBalanceGrid" SelectionMode="Single" IsReadOnly="True" 
              ItemsSource="{Binding BalanceResponse}" AutoGenerateColumns="True">

              <DataGrid.Columns>
                       <DataGridTextColumn Header="Account ID" Binding="{Binding accountId}" > 
                       </DataGridTextColumn>
              </DataGrid.Columns>

    </DataGrid>

Method:

     private async Task PopulateAccount()
    {
        BalanceResponse balanceResponse = new BalanceResponse();
        balanceResponse = await api.GetAccountBalance();
        accountBalanceGrid.DataContext = balanceResponse;
    }

Balance Response Class:

    [System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
public partial class BalanceResponse
{

    private string accountIdField;
    private string accountTypeField;
    private string optionLevelField;
    private string accountDescriptionField;
    private string quoteModeField;
    private string dayTraderStatusField;
    private string accountModeField;
    private BalanceResponseCash cashField;
    private BalanceResponseComputed computedField;
    private BalanceResponseMargin marginField;

    public string accountId
    {
        get {return this.accountIdField;}
        set {this.accountIdField = value;}
    }

    /// <remarks/>
    public string accountType
    {
        get{return this.accountTypeField;}
        set{this.accountTypeField = value;}
    }
    public string optionLevel
    {
        get{return this.optionLevelField;}
        set{this.optionLevelField = value;}
    }
    public string accountDescription
    {
        get{return this.accountDescriptionField;}
        set{this.accountDescriptionField = value;}
    }
    public string quoteMode
    {
        get{return this.quoteModeField;}
        set{this.quoteModeField = value;}
    }
    public string dayTraderStatus
    {
        get{return this.dayTraderStatusField;}
        set{this.dayTraderStatusField = value;}
    }
    public string accountMode
    {
        get{return this.accountModeField;}
        set{this.accountModeField = value;}
    }
    public BalanceResponseCash Cash
    {
        get{return this.cashField;}
        set{this.cashField = value;}
    }
    public BalanceResponseComputed Computed
    {
        get{return this.computedField;}
        set{this.computedField = value;}
    }

    public BalanceResponseMargin Margin
    {
        get{return this.marginField;}
        set{this.marginField = value;}
    }
}

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class BalanceResponseCash
{
    private decimal fundsForOpenOrdersCashField;
    private decimal moneyMktBalanceField;
    public decimal fundsForOpenOrdersCash
    {
        get{return this.fundsForOpenOrdersCashField;}
        set{this.fundsForOpenOrdersCashField = value;}
    }
    public decimal moneyMktBalance
    {
        get{return this.moneyMktBalanceField;}
        set{this.moneyMktBalanceField = value;}
    }
}

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class BalanceResponseComputed
{

    private decimal cashAvailableForInvestmentField;
    private decimal netCashField;
    private decimal cashBalanceField;
    private decimal settledCashForInvestmentField;
    private decimal unSettledCashForInvestmentField;
    private decimal fundsWithheldFromPurchasePowerField;
    private decimal fundsWithheldFromWithdrawalField;
    private decimal marginBuyingPowerField;
    private decimal cashBuyingPowerField;
    private decimal dtMarginBuyingPowerField;
    private decimal dtCashBuyingPowerField;
    private decimal shortAdjustBalanceField;
    private decimal regtEquityField;
    private decimal regtEquityPercentField;
    private decimal accountBalanceField;
    public decimal cashAvailableForInvestment
    {
        get{return this.cashAvailableForInvestmentField;}
        set{this.cashAvailableForInvestmentField = value;}
    }
    public decimal netCash
    {
        get{return this.netCashField;}
        set{this.netCashField = value;}
    }
    public decimal cashBalance
    {
        get{return this.cashBalanceField;}
        set{this.cashBalanceField = value;}
    }
    public decimal settledCashForInvestment
    {
        get{return this.settledCashForInvestmentField;}
        set{this.settledCashForInvestmentField = value;}
    }
    public decimal unSettledCashForInvestment
    {
        get{return this.unSettledCashForInvestmentField;}
        set{this.unSettledCashForInvestmentField = value;}
    }
    public decimal fundsWithheldFromPurchasePower
    {
        get{return this.fundsWithheldFromPurchasePowerField;}
        set{this.fundsWithheldFromPurchasePowerField = value;}
    }
    public decimal fundsWithheldFromWithdrawal
    {
        get{return this.fundsWithheldFromWithdrawalField;}
        set{this.fundsWithheldFromWithdrawalField = value;}
    }
    public decimal marginBuyingPower
    {
        get{return this.marginBuyingPowerField;}
        set{this.marginBuyingPowerField = value;}
    }
    public decimal cashBuyingPower
    {
        get{return this.cashBuyingPowerField;}
        set{this.cashBuyingPowerField = value;}
    }
    public decimal dtMarginBuyingPower
    {
        get{return this.dtMarginBuyingPowerField;}
        set{this.dtMarginBuyingPowerField = value;}
    }
    public decimal dtCashBuyingPower
    {
        get{return this.dtCashBuyingPowerField;}
        set{this.dtCashBuyingPowerField = value;}
    }
    public decimal shortAdjustBalance
    {
        get{return this.shortAdjustBalanceField;}
        set{this.shortAdjustBalanceField = value;}
    }
    public decimal regtEquity
    {
        get{return this.regtEquityField;}
        set{this.regtEquityField = value;}
    }
    public decimal regtEquityPercent
    {
        get{return this.regtEquityPercentField;}
        set{this.regtEquityPercentField = value;}
    }
    public decimal accountBalance
    {
        get{return this.accountBalanceField;}
        set{this.accountBalanceField = value;}
    }
}

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class BalanceResponseMargin
{

    private decimal dtCashOpenOrderReserveField;
    private decimal dtMarginOpenOrderReserveField;
    public decimal dtCashOpenOrderReserve
    {
        get{return this.dtCashOpenOrderReserveField;}
        set{this.dtCashOpenOrderReserveField = value;}
    }
    public decimal dtMarginOpenOrderReserve
    {
        get{return this.dtMarginOpenOrderReserveField;}
        set{this.dtMarginOpenOrderReserveField = value;}
    }
}

XML Example:

<BalanceResponse>
   <accountId>83564979</accountId>
   <accountType>PDT_ACCOUNT</accountType>
   <optionLevel>LEVEL_4</optionLevel>
   <accountDescription>KRITHH TT</accountDescription>
   <quoteMode>6</quoteMode>
   <dayTraderStatus>PDT_MIN_EQUITY_RES_1XK</dayTraderStatus>
   <accountMode>PDT ACCOUNT</accountMode>
   <Cash>
      <fundsForOpenOrdersCash>0</fundsForOpenOrdersCash>
      <moneyMktBalance>0</moneyMktBalance>
   </Cash>
   <Computed>
      <cashAvailableForInvestment>0</cashAvailableForInvestment>
      <netCash>93921.44</netCash>
      <cashBalance>93921.44</cashBalance>
      <settledCashForInvestment>0</settledCashForInvestment>
      <unSettledCashForInvestment>0</unSettledCashForInvestment>
      <fundsWithheldFromPurchasePower>0</fundsWithheldFromPurchasePower>
      <fundsWithheldFromWithdrawal>0</fundsWithheldFromWithdrawal>
      <marginBuyingPower>0</marginBuyingPower>
      <cashBuyingPower>93921.44</cashBuyingPower>
      <dtMarginBuyingPower>0</dtMarginBuyingPower>
      <dtCashBuyingPower>0</dtCashBuyingPower>
      <shortAdjustBalance>0</shortAdjustBalance>
      <regtEquity>0</regtEquity>
      <regtEquityPercent>0</regtEquityPercent>
      <accountBalance>0</accountBalance>
   </Computed>
   <Margin>
      <dtCashOpenOrderReserve>0</dtCashOpenOrderReserve>
      <dtMarginOpenOrderReserve>0</dtMarginOpenOrderReserve>
   </Margin>
</BalanceResponse>
8
  • What is your actual question? The ItemsSource is supposed to be set or bound to an IEnumerable. Commented Dec 2, 2020 at 15:08
  • What am I doing wrong in the XAML? It is not displaying any values. Commented Dec 2, 2020 at 15:09
  • You set the DataContext to a BalanceResponse which doesn't seem to have any BalanceResponse property that you are trying to bind to. Why would it display any values? Commented Dec 2, 2020 at 15:10
  • Where is your collection or enumerable of accounts to begin with? Commented Dec 2, 2020 at 15:11
  • 1
    Try this: accountBalanceGrid.ItemsSource = new BalanceResponse[1] { balanceResponse }; Commented Dec 2, 2020 at 15:29

1 Answer 1

1

The ItemsSource is supposed to be set or bound to an IEnumerable. You set the DataContext to a BalanceResponse object which doesn't seem to have any BalanceResponse property that you are trying to bind to:

ItemsSource="{Binding BalanceResponse}"

If there is no collection or enumerable of responses, you have to create one yourself:

accountBalanceGrid.ItemsSource = new BalanceResponse[1] { balanceResponse };
Sign up to request clarification or add additional context in comments.

Comments

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.