Wpf 绑定到XAML中声明的新IENumerable而不是CodeBehind
下面的XAML是不正确的,但它代表了我正在尝试做的事情。我是WPF的新手,但我想知道这样的事情是否可行,而不需要在代码中声明实例Wpf 绑定到XAML中声明的新IENumerable而不是CodeBehind,wpf,xaml,itemssource,Wpf,Xaml,Itemssource,下面的XAML是不正确的,但它代表了我正在尝试做的事情。我是WPF的新手,但我想知道这样的事情是否可行,而不需要在代码中声明实例 <ListBox Name="lbInstalledFonts" VerticalContentAlignment="Center" DisplayMemberPath="Name" ItemsSource="{Binding Source={new System.Drawing.Text.InstalledFontCollection().Familes}}"
<ListBox Name="lbInstalledFonts" VerticalContentAlignment="Center" DisplayMemberPath="Name" ItemsSource="{Binding Source={new System.Drawing.Text.InstalledFontCollection().Familes}}" />
为了详细说明,我只需要此列表框的System.Drawing.Text.InstalledFontCollection.Familes列表。就这样。所以我想我可以完全通过XAML来实现,就像通过使用aspx/ascx文件中的绑定来绑定ASP.NET中的数据源一样?这有可能吗?我做了一些搜索,但运气不好。是的,有可能:
<Window x:Class="HelpStackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:drawing="clr-namespace:System.Drawing.Text;assembly=System.Drawing"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<drawing:InstalledFontCollection x:Key="InstalledFontCollection" />
</Window.Resources>
<Grid>
<ListBox Name="lbInstalledFonts"
VerticalContentAlignment="Center"
DisplayMemberPath="Name"
ItemsSource="{Binding Source={StaticResource InstalledFontCollection}, Path=Families}" />
</Grid>
</Window>