#c# #wpf
#c# #wpf
Вопрос:
Этот XML десериализуется должным образом с веб-сайта и возвращается как объект класса. Например, я могу вызвать balanceResponse.AccountId в моем методе, и он отобразит значение, поэтому я знаю, что он работает. Я использую тот же процесс для других веб-запросов, и у меня не было проблем с привязкой их к сетке данных WPF.
Что я делаю не так в XAML? Он не отображает никаких значений.
Кстати, класс XML был сгенерирован с помощью специальной copy paste в Visual Studio.
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>
Метод:
private async Task PopulateAccount()
{
BalanceResponse balanceResponse = new BalanceResponse();
balanceResponse = await api.GetAccountBalance();
accountBalanceGrid.DataContext = balanceResponse;
}
Сбалансировать класс ответа:
[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:
<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>
Комментарии:
1. Каков ваш актуальный вопрос? Предполагается
ItemsSource
, что он должен быть установлен или привязан кIEnumerable
.2. Что я делаю не так в XAML? Он не отображает никаких значений.
3. Вы устанавливаете значение
DataContext
aBalanceResponse
, которое, похоже, не имеет никакогоBalanceResponse
свойства, к которому вы пытаетесь привязаться. Почему он должен отображать какие-либо значения?4. С чего начать вашу коллекцию или перечисляемые учетные записи?
5. Попробуйте это:
accountBalanceGrid.ItemsSource = new BalanceResponse[1] { balanceResponse };
Ответ №1:
Предполагается ItemsSource
, что он должен быть установлен или привязан к IEnumerable
. Вы устанавливаете DataContext
BalanceResponse
объект, который, похоже, не имеет никакого BalanceResponse
свойства, к которому вы пытаетесь привязать:
ItemsSource="{Binding BalanceResponse}"
Если нет коллекции или перечислимых ответов, вы должны создать его самостоятельно:
accountBalanceGrid.ItemsSource = new BalanceResponse[1] { balanceResponse };