绑定到在 XAML NOT CodeBehind 中声明的新 IENumerable
Binding to a new IENumerable declared in XAML NOT CodeBehind
下面的 XAML 是不正确的,但它代表了我正在尝试做的事情。我是 WPF 的新手,但我想知道如果不在后面的代码中声明实例,这样的事情是否可行?
<ListBox Name="lbInstalledFonts" VerticalContentAlignment="Center" DisplayMemberPath="Name" ItemsSource="{Binding Source={new System.Drawing.Text.InstalledFontCollection().Familes}}" />
为了详细说明,我只需要此 ListBox 的 System.Drawing.Text.InstalledFontCollection().Familes
列表。而已。所以我在想我可以完全通过 XAML 以类似于在 ASP.NET 中绑定到 DataSource 的方式来完成它(通过在 [=17= 中使用 <% 和 %> ] 文件)?这是可能吗?我做了一些搜索,但运气不佳。
是的,有可能:
<Window x:Class="HelpWhosebug.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>
下面的 XAML 是不正确的,但它代表了我正在尝试做的事情。我是 WPF 的新手,但我想知道如果不在后面的代码中声明实例,这样的事情是否可行?
<ListBox Name="lbInstalledFonts" VerticalContentAlignment="Center" DisplayMemberPath="Name" ItemsSource="{Binding Source={new System.Drawing.Text.InstalledFontCollection().Familes}}" />
为了详细说明,我只需要此 ListBox 的 System.Drawing.Text.InstalledFontCollection().Familes
列表。而已。所以我在想我可以完全通过 XAML 以类似于在 ASP.NET 中绑定到 DataSource 的方式来完成它(通过在 [=17= 中使用 <% 和 %> ] 文件)?这是可能吗?我做了一些搜索,但运气不佳。
是的,有可能:
<Window x:Class="HelpWhosebug.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>