Wpf 从xaml绑定到System.Windows枚举
我正在尝试将ComboBox ItemsSource绑定到System.Windows命名空间中的TextWrapping枚举。最终结果将是一个下拉列表,用户可以在其中选择对我的应用程序中的给定对象应用哪种类型的文本换行。当我绑定到自定义枚举时,一切正常,但我无法确定需要使用什么路径/源绑定到System.Windows命名空间中的枚举。如何通过数据绑定访问此命名空间Wpf 从xaml绑定到System.Windows枚举,wpf,xaml,enums,Wpf,Xaml,Enums,我正在尝试将ComboBox ItemsSource绑定到System.Windows命名空间中的TextWrapping枚举。最终结果将是一个下拉列表,用户可以在其中选择对我的应用程序中的给定对象应用哪种类型的文本换行。当我绑定到自定义枚举时,一切正常,但我无法确定需要使用什么路径/源绑定到System.Windows命名空间中的枚举。如何通过数据绑定访问此命名空间 <DataTemplate DataType="{x:Type MyObjectWrapper}
<DataTemplate
DataType="{x:Type MyObjectWrapper}"
>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text Wrapping" VerticalAlignment="Center" Margin="5,5,0,5"/>
<ComboBox
ItemsSource="{Binding Source={???}, Converter={local:MyEnumConverter}}"
SelectedValuePath="Value"
DisplayMemberPath="Description"
SelectedValue="{Binding Path = TextWrapping}"
VerticalAlignment="Center"
Margin="5"
/>
</StackPanel>
</DataTemplate>
更新:我的enum converter只需要在xaml中传递enum类,对于自定义enum如下所示:
<ComboBox
ItemsSource="{Binding Path=MyCreatedEnum, Converter={local:MyEnumConverter}}"
SelectedValuePath="Value"
DisplayMemberPath="Description"
SelectedValue="{Binding Path = TextWrapping}"
VerticalAlignment="Center"
Margin="5"
/>
在web上的此链接下找到了此链接 下面是修改后的代码,以满足您的需求
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<local:EnumToListConverter x:Key="enumToListConv" />
</Grid.Resources>
<ComboBox
Margin="5"
VerticalAlignment="Center"
ItemsSource="{Binding Source={local:EnumBindingSource {x:Type sysWin1:TextWrapping}}, Converter={StaticResource enumToListConv}}"
SelectedValuePath="Value" />
</Grid>
我认为这取决于您的转换器需要什么作为输入。@Alex.Wei好的一点,我更新了我的问题。出于格式方面的原因,我更愿意使用我自己的转换器。另外,我想对System.Windows命名空间中的各种枚举执行多次相同的过程,因此我不想为每个枚举创建对象数据提供程序。我不确定为什么我的C#代码没有格式化。我想您可以使用此解决方案来处理转换器,也不需要创建ObjectDataProvider,只需使用系统枚举和转换器即可。@karmjitsingh这是您的突出显示;)相关阅读:非常感谢@grek40I做了一些调整,但成功了!对于我的转换器,我不需要EnumBindingSource转换器。我还意识到默认情况下System.Windows名称空间是链接的,因此在文本包装之前不需要“sysWin1”(或类似的名称空间)。所以我的itemsource看起来像这样:ItemsSource=“{Binding Source={x:Type TextWrapping},Converter={local:MyEnumConverter}”