我有一个wpf数据网格,其中一列包含一个自动完成框,当输入帐号时,它应该更新下一列中的描述。对于我的生活,我无法让它发挥作用。我能够在自动完成框中进行选择,并更新绑定对象,但描述永远不会更新。我在父模型和子模型以及ViewModel上使用(或至少尝试使用)INotifyPropertyChanged(通过PropertyChanged.Fody),但我不确定我是否正确使用它。绑定似乎正在起作用,但我认为问题在于,当在自动选择中进行选择时,会在行上更改帐号属性,但不会在行和帐户对象之间建立实际关联。由于没有关联,描述仍然是空白的。至少这是我的理论。如何让描述字段正确更新?
这是设置父母和孩子的方式。
这是父模型:
public class Transaction : RealmObject, INotifyPropertyChanged
{
// PropertyChanged.Fody
public event PropertyChangedEventHandler PropertyChanged;
[PrimaryKey]
public string ID { get; set; } = Guid.NewGuid().ToString(); // For Realm
public string ReportBasis { get; set; }
public string ReportName { get; set; }
public DateTimeOffset StartPeriod { get; set; }
public DateTimeOffset EndPeriod { get; set; }
public DateTimeOffset RunTime { get; set; }
public IList<TransactionDetails> Rows { get; }
}
这就是孩子:
public class TransactionDetails : RealmObject, INotifyPropertyChanged
{
Public event PropertyChangedEventHandler PropertyChanged;
[PrimaryKey]
public string ID { get; set; } = Guid.NewGuid().ToString(); // For Realm
public Account Account { get; set; }
public string Comment { get; set; }
public double? DebitAmount { get; set; }
public double? CreditAmount { get; set; }
public TransactionDetails()
{
this.Account = new Account() { RawAccountNumber = "", AccountKey = "", Description = "" };
}
}
这是帐户对象
public class Account : RealmObject, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[PrimaryKey]
public string ID { get; set; } = Guid.NewGuid().ToString();
public string AccountKey { get; set; }
public string Description { get; set; }
public string RawAccountNumber { get; set; } //110100000
public string FormattedAccountNumber { get; set; } // 1101-00-00
public string Status { get; set; }
public DateTimeOffset DateUpdated { get; set; }
}
ViewModel只有一个事务
public class MainWindowViewModel : BaseViewModel
{
private Transaction _transaction;
public Transaction Transaction
{
get { return _transaction; }
set {
_transaction = value;
}
}
...
private async void GetTransactions()
{
var qb = new QB();
Transaction = await qb.GetTransactions( StartDate, EndDate );
}
...
}
其行通过Xaml绑定到DataGrid:
<DataGrid x:Name="TransactionsGrid"
DataGridCell.Selected="DataGrid_GotFocus"
AutoGenerateColumns="False"
HorizontalAlignment="Left"
Margin="10,51,0,0"
VerticalAlignment="Top"
Height="540"
Width="902"
ItemsSource="{Binding Transaction.Rows}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Account Number" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox BorderThickness="0"
Text="{Binding Account.RawAccountNumber}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<toolkit:AutoCompleteBox
Background="AliceBlue"
IsTextCompletionEnabled="True"
FilterMode="Contains"
MinimumPrefixLength="2"
ValueMemberPath="RawAccountNumber"
PreviewTextInput="AutoCompleteBox_PreviewTextInput"
Text="{Binding Account.RawAccountNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Accounts}" >
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RawAccountNumber}" FontWeight="Bold" Width="100"/>
<TextBlock Text="{Binding Description}" />
</StackPanel>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Account Description" Width="Auto" Binding="{Binding Account.Description}" IsReadOnly="True" /> <!-- This should update when the account is selected in the autocomplete box above -->
<Controls:DataGridNumericUpDownColumn Header="Debit" Width="Auto" Binding="{Binding DebitAmount, Mode=TwoWay }" Minimum="0" StringFormat="{}{0:C}" />
<Controls:DataGridNumericUpDownColumn Header="Credit" Width="Auto" Binding="{Binding CreditAmount, Mode=TwoWay}" Minimum="0" StringFormat="{}{0:C}" />
</DataGrid.Columns>
</DataGrid>
我正在摸索着这一点。有人可以看看这个并指出我正确的方向吗?