简体   繁体   中英

mapping Namespace in XAML not working

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 ?

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;

    }

  }
}

The solution is after adding a separate class to my project as myConverter under BlinkApplication.Converters namespace, Build the project & this adds the class into the project at the correct namespace for the xaml interpreter to find it.

Then in MainWindow.xaml, add the converter namespace at the top as

      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.

After it has been declared, I can use it in window or any other control resources.

确保您的转换器定义为“公共”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM