我已经看到这个请求略有不同 - 但我很难让它适合我的情况。我有一个应用程序,用户选择组合框将驱动一个事件。这甚至从数据库中获取数据,我想将该数据绑定到 UI。我以为我的绑定设置正确,但我什么也做不了。我可以看到我的所有代码计算都正确触发,但我没有看到任何关于绑定的反馈。我对 WPF 还很陌生,并且仍在努力解决它(我来自 MVC 网络世界),因此非常感谢任何指针。谢谢。
MainWindow 后面的代码:
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
private void cboAllClients_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MainWindowViewModel vm = DataContext as MainWindowViewModel;
listClient selectedClient = e.AddedItems[0] as listClient;
Credit.Calc c = new Credit.Calc();
c.CalcCredit(selectedClient.ClientName);
vm.FFCredits = c.FundFamilyCredits;
vm.FundCredits = c.FundCredits;
DataContext = vm;
}
主窗口 xaml(缩写为绑定问题)
<ListBox x:Name="lbFundFamily" Width="auto" Height="auto"
Background="Transparent" BorderThickness="0">
<ListBox.DataContext>
<Binding Source="FFCredits"></Binding>
</ListBox.DataContext>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=FundFamilyName}"
Grid.Row="0" Grid.Column="0"
Background="White" FontSize="14" Foreground="Black">
</TextBlock>
<!--<Rectangle Width="{Binding Path=BrokerCreditList.Utilization}"
Height="20" Grid.Row="0" Grid.Column="1"></Rectangle>-->
<!--<TextBlock Text="{Binding Path=BrokerCreditList.}"></TextBlock>-->
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
主窗口视图模型:
class MainWindowViewModel : BaseViewModel
{
public List<listClient> AllClients { get; set; }
public ObservableCollection<FundFamilySum> FFCredits { get; set; }
public ObservableCollection<FundSum> FundCredits { get; set; }
public MainWindowViewModel()
{
using (AgencyModelEntities db = new AgencyModelEntities())
{
AllClients = db.listClients.ToList();
FFCredits = new ObservableCollection<FundFamilySum>();
FundCredits = new ObservableCollection<FundSum>();
}
}
}