Reputation: 8641
I'm trying to display on a DataGrid a ObservableCollection. For each item in the ObservableCollection, I have a ComboBox that displays the currently selected item (MyColor in my example) and lets the user choose a different color from the combobox. This was working reasonably well when my Enum MyColor was in the same namespace as the MainWindow. However, to make the example closer to the problem I'm actually trying to solve I put the MyColor enum in a different project and namespace. I've included all code. In the XAML for the mainwindow, I have a ObjectDataProvider and the line
<x:Type TypeName="enu:MyColor" />
The word x:Type is underlined and has the message: The name MyColor does not exist in the namespace "clr-namespace:Library.EnumDefinitions;assembly=Library.EnumDefinitions" I've double and triple checked spelling, but don't see the problem. MyColor is defined in the namespace Library.EnumDefinitions. I'm wondering if the fact that the enum is in a separate namespace and project is causing me grief. The idea of using ObjectDataProvider can by found here for example: https://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/ The suggestion that there might be something special going on if your enum is in a different project/namespace is given here: The name does not exist in the namespace in WPF application I do have a reference to the different project in my Main project.
Any idea what is going on? If I can provide other information, please let me know.
Thanks, Dave
MainWindow 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
}
}
UPDATE!!! I believe I fixed my immediate problem. I changed the line:
xmlns:enu="clr-namespace:Library.EnumDefinitions;assembly=Library.EnumDefinitions"
to:
xmlns:enu="clr-namespace:Library.EnumDefinitions"
Can someone tell me when you are suppose to use the "assembly" part and when you are not?
Upvotes: 0
Views: 179