我正在尝试在 DataGrid 上显示 ObservableCollection。对于 ObservableCollection 中的每个项目,我都有一个 ComboBox,它显示当前选定的项目(在我的示例中为 MyColor),并让用户从组合框中选择不同的颜色。当我的 Enum MyColor 与 MainWindow 位于相同的命名空间中时,这运行得相当好。但是,为了使示例更接近我实际上试图解决的问题,我将 MyColor 枚举放在不同的项目和命名空间中。我已经包含了所有代码。在主窗口的 XAML 中,我有一个 ObjectDataProvider 和行
<x:Type TypeName="enu:MyColor" />
单词 x:Type 带有下划线并显示消息:名称 MyColor 不存在于命名空间 "clr-namespace:Library.EnumDefinitions;assembly=Library.EnumDefinitions" 我已经检查了两次和三次拼写,但没有看到问题。MyColor 在命名空间 Library.EnumDefinitions 中定义。我想知道枚举位于单独的名称空间和项目中的事实是否让我感到悲伤。可以在这里找到使用 ObjectDataProvider 的想法,例如: https : //brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/枚举位于不同的项目/命名空间中,此处给出:
知道发生了什么吗?如果我可以提供其他信息,请告诉我。
谢谢,戴夫
主窗口 XAML
<Window x:Class="TrickyBindingProblems.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:enu="clr-namespace:Library.EnumDefinitions;assembly=Library.EnumDefinitions"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider x:Key="EnumDataProvider" MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="enu:MyColor" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<DataGrid Grid.Column="1"
Margin="0"
HorizontalAlignment="Left"
AutoGenerateColumns="False"
Background="Transparent"
DataContext="{Binding}"
HeadersVisibility="Column"
ItemsSource="{Binding Cars}"
SelectedItem="{Binding SelectedItemProperty, Mode=TwoWay}"
RowBackground="Transparent"
RowHeight="30">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="Black" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Width="*" Header="Make">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Padding="5"
Text="{Binding Make}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*" Header="Color (as ComboBox)">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource EnumDataProvider}}" SelectedItem="{Binding Color, Mode=TwoWay}">
<ComboBox.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black" />
</Style>
</ComboBox.Resources>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
namespace Library.EnumDefinitions
{
public enum MyColor
{
Red,
Blue,
Green
}
}
更新!!!我相信我解决了眼前的问题。我改变了这一行:
xmlns:enu="clr-namespace:Library.EnumDefinitions;assembly=Library.EnumDefinitions"
至:
xmlns:enu="clr-namespace:Library.EnumDefinitions"
有人能告诉我你什么时候应该使用“装配”部分,什么时候不使用吗?