Question Use Dynamic Resource from the class

neivalf88

New member
Joined
May 31, 2018
Messages
4
Programming Experience
3-5
Hello there!
I am programming something with views, view models, and models, and i need to display a DataGrid. I want to put the names of the colums in a xaml file, so the user can choose a langage package.
I need to declare these colums in my ViewModel file. This work fine when I put the name directly in the header, like this:
C#:
this.actionColumns.Add(new DataGridCheckBoxColumn            {
                Header ="Actif", //We define the header
                Binding = new Binding("ActifIsChecked"), //We define the Binding
            });
But I tried to look at several forums to use my Resource file, and I didn't found anything.

If I modify my code like this:
C#:
var resource = new DynamicResourceExtension("actif");            this.actionColumns.Add(new DataGridCheckBoxColumn
            {
                Header =resource.ToString(), // On definit le header
                Binding = new Binding("ActifIsChecked"), // On definit le Binding
            });
This doesn't work, here is a screenshot of the result:
Capture.PNGAs you can see, it's written "System.Windows.DynamicResourceExtension" (basically the name of the class), and it should be written "Actif"

Thanks a lot by advance for your help (and btw, sorry if i did some mistakes in english, i'm French ;p)
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,196
Location
Sydney, Australia
Programming Experience
10+
When you call the ToString method of an object, it's up to that object what gets returned. The default implementation of the ToString method, which is in the Object class, is to simply return the name of the type, which is exactly what you're seeing. Some types override that implementation to provide their own if it makes sense for that type but most don't. Don't simply call ToString on an object and assume that it will return what you want it to or think it should. If you had read the documentation for the DynamicResourceExtension class and followed the appropriate links then you'd have seen this:
Returns a string that represents the current object. (Inherited from Object.)
Default implementations of the Object.ToString method return the fully qualified name of the object's type.
You may have reached this page by following the link from the member list of another type. That is because that type does not override Object.ToString. Instead, it inherits the functionality of theObject.ToString method.
VS has a Help menu so that you can find this sort of information quickly and easily.

In your case, I'm guessing that what you actually want is the ResourceKey property of the object. That property is itself type Object but, most likely, you have used a String as the key so you can either cast it as that type or call its ToString method and you'll get what you expect.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,196
Location
Sydney, Australia
Programming Experience
10+
By the way, your application is WPF, not Windows Forms, so I have moved it to the appropriate forum. It would also be appreciated if you could update your profile because, if you using WPF, you can't possibly be targeting .NET 1.1.
 

neivalf88

New member
Joined
May 31, 2018
Messages
4
Programming Experience
3-5
Solution found

Hello ,
Finally I found the solution by myself,
I'll share it to you guys, if one day you have the same problem. I made a function:
C#:
/// <summary>
        /// Function to find the text value associated with the key in the langage package
        /// </summary>
        /// <param name="key">The key assiociated with the resource</param>
        /// <param name="path">The Uri code of your file's location</param>
        /// <returns>The text element assiociated with the key</returns>
        public string getResource(string key, string path)
        {
            var resource = new ResourceDictionary();
            try { resource.Source = new Uri(path); }
            catch { MessageBox.Show("Cannot find the langage file", "Error"); }


            //Put the values in an array
            string[] elements = new string[resource.Values.Count];
            resource.Values.CopyTo(elements, 0);
            //Put the keys in an array
            string[] keys = new string[resource.Keys.Count];
            resource.Keys.CopyTo(keys, 0);


            bool found = false; //Tell if the value of the header is found to stop searching
            string result = "";
            for (int i = 0; i < elements.Length && found == false; i++)
            {
                if (keys.GetValue(i).ToString() == key)
                {
                    result = elements.GetValue(i).ToString();
                    found = true;
                }
            }
            return result;
        }

You may ask why I transfered the results of the resource.Values, because it's a ICollection , so you can't get a value according to an index.

Have a nice day! :distracted:
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,196
Location
Sydney, Australia
Programming Experience
10+
You can replace all this:
            //Put the values in an array
            string[] elements = new string[resource.Values.Count];
            resource.Values.CopyTo(elements, 0);
            //Put the keys in an array
            string[] keys = new string[resource.Keys.Count];
            resource.Keys.CopyTo(keys, 0);


            bool found = false; //Tell if the value of the header is found to stop searching
            string result = "";
            for (int i = 0; i < elements.Length && found == false; i++)
            {
                if (keys.GetValue(i).ToString() == key)
                {
                    result = elements.GetValue(i).ToString();
                    found = true;
                }
            }
            return result;

with this:
return resource.Contains(key) ? resource[key].ToString() : string.Empty;
 
Top Bottom