Fill dictionary with sampledata - c#

How do I fill a dictionary with sampledata to use in design mode?
I added xmlns:k="clr-namespace:System.Collections.Generic;assembly=mscorlib" to be able to add key-value pairs but somehow it still doesnt work.
Can someone help me with this please?

There is currently no proper support for generic class in XAML (in XAML 2009 there is x:TypeArguments, currently only usable in uncompiled XAML), a workaround is using custom markup extensions that create them via reflection. An example can be found in another answer of mine, you could adjust it for dictionaries so you can also add the key value pairs.

Related

Create a dictionary in c# which is strongly coupled with another dictionary

I am working with C# for a Hexagon shaped grid with bound checks and wrapping etc. The map in the library is a dictionary Dictionary<TileCoord,int> tileIndexByPosition. I also have the required function to obtain CornerCoord for each tile.
I would like to create a dictionary Dictionary<CornerCoord,int> cornerIndexByPosition whose values depend on tileIndexByPosition. Whenever tileIndexByPosition changes its elements creating a new dictionary for corners seems quite inefficient.
The idea I have is to use the Lazy evaluation of LINQ to strongly couple the two dictionary. Would like any advice on how to approach this problem.
EDIT: I use these dictionary to check if a TileCoord or CornerCoord is part of the map by using the ContainsKey.
I would use a KeyValuePair like this
KeyValuePair<Dictionary<TileCoord,int>,Dictionary<CornerCoord,int>> kvp;
If you change the Key ( tileIndexByPosition ) you also can change the Dictonary Value of the KeyValuePair Value that depends on the KeyValuePair Key.
This is like you said you want. But I think you want something like this:
Dictonary<KeyValuePair<TileCoord,int>,KeyValuePair<CornerCoord,int>> dic;
I hope this helps.
Kind regards

How can I do compiled binding or x:Bind to indexed property using string as an index?

I'm trying to use compiled binding I have a property Errors that I used to bind using the regular binding like {Binding Errors[PropertyName]}. However, when I tried to use {x:Bind VM.Errors[PropertyName]}, I got this error
"Invalid binding path 'VM.Errors[PropertyName]' : Expected a digit"
I also tried to use quotes like VM.Errors['PropertyName'] but it does'nt solve the problem.
It does not work. Microsoft writes:
{Binding Groups[2].Title}
Binds to the specified item in the collection. Only integer-based indexes are supported.
See: https://msdn.microsoft.com/en-us/library/windows/apps/mt210946.aspx at the end of the page. I event tried implementing IReadOnlyDictionary oe IDictionary, but no success.
Its interessting because on https://msdn.microsoft.com/en-us/library/windows/apps/mt185586.aspx they say:
For example, consider a business object where there is a list of "Teams" (ordered list), each of which has a dictionary of "Players" where each player is keyed by last name. An example property path to a specific player on the second team is: "Teams1.Players[Smith]". (You use 1 to indicate the second item in "Teams" because the list is zero-indexed.)
Update
My support case at Microsoft connnect was closed. They write:
Thank you for reporting this issue as well as providing a sample project. The suggested scenario was not supported in Windows 10 RTM. We are however considering adding such support to a future update to Windows 10.

Dynamically create a class and then create a list<dynamicClass> based on it in C#

I want to create a class and its properties on run time, the properties will be like Year2001, Year2002, Year2003, Year2004, Year2005... I get these property names on run-time, I get them in a list. Later I need to use this class to create a list which I need to show in the kendo grid.I surfed a lot and thought of using ExpandoObject, but was unsuccessful.
If all properties will be of the form YearX and contain some information about or related to that year, then I would strongly recommend you (if at all possible) to go with something along the lines of an IList<YearInfo> where YearInfo is some object containing the info you need for every year, including an integer property indicating what year the object corresponds to. If you require these objects to be unique you could use an IDictionary<int, YearObject> or ISet<YearObject> instead.
Reflection can be powerful, but it it comes at the price of complexity and loss of type safety/compile-time checks. Avoid when possible.
Sounds to me like you are really wanting to a grid with grouping support. Your idea of having the system create a CLASS at runtime is not going to fly. Even if it were possible, which I doubt it is, it is absolutely the wrong approach.
Like I say - have a read about Grouping / Hierarchy on Grid Controls (Kendo grid example here), and maybe have a look at OLAP cubes as well...
Although you have had some answers I would also like to suggest an alternative way of doing this which is using DataTables. This is the approach I take when I have any "Dynamic" data sets that I want to present to the grid.
This is also the approach that Telerik themselves take with one of their code samples.
here are a couple of links to show them doing this to DataTables and Dynamic Objects
Grid Binding to Data Table
Grid Binding to Dynamic Objects
Personally I find the binding to Tables easier to deal with as I am used to dealing with Data Tables.

Templated Lists in C#?

I know templates are common within c/c++ however I am currently trying to translate an old VB code our company uses up to a c# equivalent which brings me to my problem....
VB is an type unsafe language so we come across things like
Public Elements As New Collection
so given the line above I need to translate that to a List.. Given that a Collection can be anything from a List to a map I need to template this as efficiently as possible. I was thinking of for first draft ignoring the map option entirely and just making it a List however from my understanding Templates don't truly exist within C#... The below code is what I came up with so far and while it compiles (I have not gotten to a testing point in the rest of the code yet) I don't know if it would actually work...
Public List<ValueType> Elements = new List<ValueType>();
can anyone offer input on if this would work as a generic list where type is determined at input or if I need to change this so the constructor would look more like
Public List<ValueType> Elements = new List<typeof(foo)>();
if the above is confusing I am sorry it is for me as well, and I will try and clarify as questions come in.
this question is no longer relevant, I was able to go into the source of the calling code and determine what variable types that the lists need to support.

Can I use the Dictionary type as a (persistet) property of a custom server control?

I'm currently developing a server control which should be configured via a list of key/value pairs, like this:
<MyControls:ContentRenderer ID="ContentRenderer1" runat="server">
<MyControls:Placeholders>
<MyControls:Placeholder key="ident1">Some text which replaces {ident1}</MyControls:Placeholder>
<MyControls:Placeholder key="ident2">Some text which replaces {ident2}</MyControls:Placeholder>
</MyControls:Placeholders>
<MyControls:ContentRenderer />
I want that property to be a dictionary so I can quickly retrieve placeholder mappings by their identifier. I know how to create a property which is persisted with the above markup by using the List< T > class but I'd like to have a hashmap-like datastructure.
I read a lot of stuff at msdn but I still have no clue what to do if you want full control over the way your control markup is parsed.
I don't think dictionaries are supported in this way. However, the way this is done, is instead of using List, use a custom list-based class, which also stores a dictionary mapping of the key/object.
HTH.

Categories

Resources