tags:

views:

282

answers:

2

The following is a snippet from a xaml defining a DataGrid in a Control, defining a template selector.

<DataGrid.Resources>
    <selector:CurrencyColorSelector x:Key="currencyColorSelector">
        <selector:CurrencyColorSelector.NegativeTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Balance, StringFormat=n}" Background="Red"/>
            </DataTemplate>
        </selector:CurrencyColorSelector.NegativeTemplate>
        <selector:CurrencyColorSelector.NormalTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Balance, StringFormat=n}"/>
            </DataTemplate>
        </selector:CurrencyColorSelector.NormalTemplate>
    </selector:CurrencyColorSelector>
</DataGrid.Resources>

Now, an error is thrown: "Unknown build error, 'Path cannot be null. Parameter name: path Line 27 Position 79.'" (Compiler or xaml validation error).

Edit

After a reboot of Visual Studio 2010 I get the following:

The tag 'CurrencyColorSelector' does not exist in XML namespace 'clr-namespace:EveTrader.Wpf.Selectors;assembly=EveTrader.Wpf'. Line 27 Position 18.

Which makes it even weirder, as I even have intelisense for the class.


I have no idea where this Path comes from, neither does my example show anything of it. If you doubleclick the error, it points to the end of <selector:CurrencyColorSelector x:Key="currencyColorSelector"> (line 27).

Did anybody encounter such a problem and has a solution for it? The example was from here: http://www.wpftutorial.net/DataGrid.html (Row Details depending on the type of data)

Clarification

This is under .net 4.0. The Problem has to do with the CurrencyColorSelector, as the Templates themselves work fine if used in the DataGridTemplateColumn on their own. CurrencyColorSelector derives from DataTemplateSelector.

CurrencyColorSelector

namespace EveTrader.Wpf.Selectors
{
    public class CurrencyColorSelector : DataTemplateSelector
    {
        public DataTemplate NegativeTemplate { get; set; }
        public DataTemplate NormalTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var data = item as DisplayWallets;

            if (data == null)
                return base.SelectTemplate(item, container);

            if (data.Balance < 0m)
                return NegativeTemplate;
            return NormalTemplate;
        }
    }
}

DisplayWallets

public class DisplayWallets
{
    public string Name { get; set; }
    public decimal Balance { get; set; }
}

Xaml selector definition

<UserControl x:Class="EveTrader.Wpf.WalletsView"
             xmlns:selector="clr-namespace:EveTrader.Wpf.Selectors;assembly=EveTrader.Wpf">
A: 
Igor Zevaka
I'm using .net 4.0. Changing the Binding to Path= didn't change anything. Besides, the problem only started to appear with the resource - using the StringFormat in other places worked like a charm.
Femaref
Right, must be something else then.
Igor Zevaka
updated it... it gets even weirder.
Femaref
I fixed it, the solution is surprisingly simple.
Femaref
A: 

Okay, I fixed it. The problem was the definition of selector:

xmlns:selector="clr-namespace:EveTrader.Wpf.Selectors;assembly=EveTrader.Wpf".

As the type CurrencyColorSelector resides in the EveTrader.Wpf assembly, this definition created a circular reference which created the error - the compiler tried to compile the assembly EveTrader.Wpf, but because of the defition of selector, it tried to create EveTrader.Wpf first. This continued ad nauseam. The fix is simple: remove the assembly definition: xmlns:selector="clr-namespace:EveTrader.Wpf.Selectors".

I fixed this problem after some hours of sleep after working through the night, which proves again, sleep is needed. Thanks for the help Igor anyway.

Femaref