Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
I have created a datagrid which displays a Table of records populating from a Database
& would like to animate the cells of the datagrid when certain condition is met.
For this I created a converter class named **BlinkConverter** that inherits IValueConverter.

to put this converter into action, I have mapped the project namespace onto the xaml editor as

           xmlns:local="clr-namespace:BlinkApplication"

Note: BlinkApplication is the name of my Project

After adding this, I am trying to create an instance of my BlinkConvertor class for Binding with Windows.Resources collection as

            <Window.Resources>
            <local:BlinkConverter x:key="Blink"></local:BlinkConverter>
            </Window.Resources>

here my Intellisense is not detecting the class BlinkCoverter after I type *"local: "* , even if I try to type, I have an error stating "The type local:BlinkConverter was not found. Verify that you are missing an assembly reference and that all referenced assemblies have been built."

Even though I have added the entire project under the xmlns in my xaml editor .
What is wrong here ? have I missed any reference ?
Posted

If you have added the converter and have not built the project, you will get that error. Any new classes you add to the project must be built before they will be picked up by the xaml interpreter.

Secondly, make sure you don't have a namespace problem. For example, if you created a behaviors folder and you have the cs file for the behavior in that folder, Visual Studio would have likely put that class at BlinkApplication.Behaviors. Your :local reference will have to match that. Look at the top of the file containing the converter class and see if there is a namespace declaration.

The best thing I've found is to add the code for the converter, build the project, then go into the xaml and add the namespace. Intellisense will pop-up the available ones and you can find it in there. That ensures you've got it declared properly.

Finally, make sure you declared your converter Public. May seem silly but it won't be available in xaml if you don't.
 
Share this answer
 
Comments
BuBa1947 10-Dec-12 12:51pm    
Do I have to add the Converter class as a part of the MainWindow.xaml.cs class or add a new class naming Converter and then mapping it to the MainWindow.xaml.cs class ?

Because on the first try, I added the as a part of Mainwindow.xaml.cs on the first try, then my Intellisense didn't detect, but when I added a separate class as Converter.cs , my Intellisense detects but I dont know the way to map to my Mainwindow.xaml.cs class :(

Converter.cs

public class Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string cellvalue = value.ToString();
return cellvalue = ("Pass");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return false;
}

MainWindow.xaml.cs

namespace BlinkApplication
{
public partial class MainWindow : Window
{
SqlConnection cn;
SqlDataAdapter da;
DataSet ds;
public MainWindow()
{
InitializeComponent();
DataContext = this;
cn = new SqlConnection(@"Data Source=CZC0239ZWZ\SQLEXPRESS; Initial Catalog =Student; Integrated Security=true");
cn.Open();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
da = new SqlDataAdapter("select * from dbo.View_StudentResults",cn);
ds = new DataSet();
da.Fill(ds);
dataGrid1.ItemsSource=ds.Tables[0].DefaultView;

}

}
}
Jason Gleim 10-Dec-12 13:51pm    
Your public class Converter is correct... although I wouldn't call it 'Converter'... something less likely to collide with a keyword is probably a better idea... like "myConverter".

So you have MyConverter.cs and in it you have:

namespace BlinkApplication.Converters
{
public class myConverter:IValueConverter
{
...
}
}

After you add that to the project (as it seems you have done) build the project. You should build successfully. This adds the class into the project at the correct namespace for the xaml interpreter to find it. If you don't do this, you won't get intellisence in xaml and VS will tell you the class doesn't exist.

In MainWindow.xaml, you'll add the converter namespace at the top..
xmlns:local="clr-namespace:BlinkApplication.Converters"
Notice that it matches the namespace as declared in the Converters.cs file? That associates the "local" tag with the BlinkApplication.Converters namespace.

Once you have done that, you can declare the converter in the page/window/user control resources:
<usercontrol.resources>
<local:myconverter key="myValueConverter">


Finally... use the converter in the code:

<textblock text="{Binding" somebinding,="" converter="{StaticResource" myvalueconverter}}="">

You should not do anything in the code behind (MainWindow.xaml.cs). Although you CAN assign this stuff in the code behind, it is meant to be declaratively defined in xaml.
BuBa1947 11-Dec-12 4:57am    
Thanks a lot. It worked finally and I got a better understanding..
Cheers :)
-Indhira
Are you using VS2010? I also experience that sometimes, and check what I am actually missing, but I guess VS is experiencing bug. Try to rebuild and ignore the intellisense it will be recognize later on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900