Trying to build a Query Builder Control in WPF - c#

Please bear with me for the length of this question, I tried to be as descriptive as possible. I am new to WPF programming. I am basically trying to build the RadDataFilter control:
RadDataFilter is used to filter collection by building query expression. The control is generally used by bringing all data from the database and then filtering on them.
However, I am trying to use it to build SQL query expression and then I query the database to retrieve the data. Our company has license for Telerik products so I had to try and use the control. Unfortunately, the control is not flexible to the extent that I would want to customize the Left Dropdown (RadComboBox).
As we have several items which users can filter on, it is not feasible to provide them with a single dropdpwn. I would like to have a custom control instead of the dropdown.
I have the following workaround to this problem:
Extract the Template for that control and override it in my XAML as described here. However, inspite of doing this, I am unable to change the control. I assume it is due to my inadequate knowledge in WPF.
Try to change the Source code for Telerik (We have the license for the code too). However with this approach, we need to keep making changes with every upgrade, which is high maintenance for the team.
Try to build a custom control.
I am looking for the 3rd approach. If there is an easy way (using the first 2 approaches) or any other solution to achieve this, please let me know and I shall try that.
To implement the 3rd approach, I am thinking to use a TreeView with each node using a ControlTemplate (containing my Custom Control). I will override the ToString() method of the Custom Control that gives me the one query condition. Once done, I would iterate through the children of the root node to build the final query expression.
However as I notice, we need 2 different ControlTemplates (one which allows creating a filter expression and the other with the operator, similar to RadDataFilter). I do not know how to set multiple templates like this.
This may sound simple to some of the developers out there, but I am really new to C# and WPF programming and would need time and experience to be comfortable with it.
Please let me know if you know any other solution that I can try to solve this problem. If you feel it is better to create a custom control, please guide me on whats the best way to implement this. Also comment on my approach to create the custom control.
Thanks!

I resolved it. Took the first approach:
"Extract the Template for that control and override it in my XAML as described here.."

Related

Implementing a refactoring system for my autogenerated code

I have a little visual system for generation FSM's where the user can draw a graph using boxes (states) and link them with lines (transitions). This, in the end, generates c# code when user presses the "Generate code" button that defines the FSM in runtime.
I want my users to be able to change things like graph name, transitions names, states names, delete nodes, delete transitions and a bit more after the first save, so, I need a way to handle refactoring.
I'm struggling trying to find a non intrusive way to accomplish this. Have tried to apply a modification of a do/redo algorithm I made some time ago but couldn't be able to get something nice.
Could anyone explain how to create such a system, making it as less intrussive with existent code as possible?
Cheers.
I would suggest keeping the state in your graph datastructure, and generating the C# code anew on changes to the FSM, this is a simple solution that will allow arbitrary modification of the FSM-datastructure without having to worry about applying said modifications to the generated code.
For implementing 'refactorings' of the base FSM-data structure, you could use something like a Command Pattern to encapsulate the refactorings and undo/redo operations.

ASP.NET MVC 3 Razor View Restrictions

I apologize in advance for the generic nature of my question, but I was unable to find any helpful advice from people trying to do the same thing as me on the web. Let me describe my scenario:
I am providing end users/designers of a website the ability to customize their views by storing the views (using Razor) in the database. I have all of this working, but my question is the following; From a security standpoint, how can I ensure and enforce that unwanted code doesn't get executed in the user-defined view? There are two basic approaches that I think will work conceptually, but am not sure which one is more possible or feasible.
Option 1: Create a validation method in the administration tool that allows the user to input the view code. This would need to either take a whitelist or blacklist approach to what is allowable or not.
Option 2: Prevent unwanted code from being able to execute when rendering of the view occurs.
As a quick example of something that would need to be blocked, we wouldn't want to allow access to read or write files, access any data access functions, or even access configuration settings, etc. in the web.config. There will likely be a decently-sized list of things that probably shouldn't be allowable, but I'll need to sit down and try to think of as many security-related concerns as possible.
My question then is, which method would be the best bet? Also, can any direction be provided on how to go about either? I thought I might be able to make trust-level based change which would be Option 2, but couldn't find any way to make that work in a per-view based manor (the administration code is allowed to execute whatever it wants). I'm thinking Option 1 will end up being the best bet and I'll have to check for the input of certain framework functions that shouldn't be allowed. Does anyone have any experience doing anything like what I'm trying to do? ANY feedback is much appreciated!
This would be extremely difficult.
You could run the the template through the Razor preprocessor, then use Roslyn (still in early beta) to parse the generated file and look through all method calls (or constructors) and return an error if it calls something you don't like.
I strongly recommend that you use a whitelist for that, since the .Net framework is big enough that you are bound to overlook something in a blacklist.
However, I would instead recommend that you not use Razor at all and instead use a templating engine that does not allow real C# code.

Callbacks or simple arithmetic in StringTemplate

I'd like to use something like this in StringTemplate - to call a function defined in the application that does the rendering, is that possible?
<div> $renderArticle(article,width,padding)$ </div>
I could use a custom type/class just for this field but that seems a bit hacked togheter and not a real solution to the problem, and it wont allow me to pass in arguments as far as i know.
I want this becuase i want to allow users to customize templates, for example: i know the how much width i have available for the article but i want the user to be able to specify padding so that i can generate optimal image thumbnails inside that template.
Or, if is it possible to use simple arithmetics, that would be another solution to the problem, something like this perhaps:
... width="$width-50$ ...
I have been looking all over for this and not found any information...
I'd rather use StringTemplate because the application already uses it, but i could switch to something else if it's more suitable, maybe i should take a look at NVelocity?
As I82Much says in his comment, StringTemplate doesn't support calculations.
One solution is to enrich your model with a new property whose value is pre-calculated as 'width-50'.
If you're looking for an alternative, and you're using .net 4.0, you can host the razor view engine and use it as your templating solution.

Building a Silverlight DataTemplate in c# code

This might be more of an outcry, but building DataTemplates in Silverlight code seems to be impossible. The one option I am aware of being using XamlReader.Load(string), one is met with several issues using this approach:
No validation, everything is done in concatenated strings. We all know this sucks
Any necessary resources have to be included, as this will run in its own context. This also sucks, although there is a logical explanation and not a show-stopper.
Any attempts to include event calls inside your DataTemplate will fail with InvalidOperationException: Invalid XAML for control: XamlReader.Load() does not accept event handlers.
No.3 seems to be a show-stopper for me. I would appreciate it if anyone gave some clues, pointers or ideas towards another / better way of doing this.
In WPF there is a FrameworkElementFactory that can provide assistance, however there seems to be no remedy for this in Silverlight.
Maybe you dont need to build datatemplates in code. Do you really need to do that?
(#2) You dont need to include the xaml resource. it could reside remotely somewhere. all you have to do is load it remotely. But maybe im not understanding what you said.

WPF advice on XML data binding: XmlDataPresenter vs custom class?

I'm just starting out in WPF and I'm building a simple movie library as a test project to help me find my feet.
I'm storing my movie information in XML, for example:
<movie id="1" title="Back To The Future" date="1985" ... />
The movies appear in a list and when clicked the fields will become editable. I've got that working by using two Data Templates to style the list items, applied via a Trigger on the ListBoxItem.IsSelected property.
But for binding and editing the data, I'd be really grateful for some advice. I've tried each of the approaches below with varying degrees of success. Which do you think makes the most sense? Are there any other things I could try?
Approach 1: Bind to XML directly using XMLDataProvider in XAML. In this case the ListBox binds to the XmlDataProvider and the individual control elements are bound via XPath expressions. It's very simple to get started, but this approach seems to become tricky when editing the data. We need rather a lot of ValueConverters just to ensure that the data is presented sensibly and saved back to the XML file in the appropriate format.
Approach 2: Create a custom Movie class that receives the XML and exposes all the movie attributes as public properties to which the controls are bound. This feels more robust and flexible, but seems to require a lot more work to implement. Every property needs to be explicitly exposed in code and validation is a pain.
Approach 3: A bit like approach 2 but with a Movie user control instead of the ListBox + Data Templates. I started with this and quickly abandoned it because I couldn't see any advantages over approach 2.
I realise it's not a straightforward question and that there are a lot of factors to consider, but any thoughts on the various pros/cons (or whether there are any tricks I'm missing) would be appreciated. Please let me know if you would like more info or code samples. And apologies if this question is inappropriate or unwelcome.
Many thanks.
If its just for displaying, whether or not you use templates, approach one is more efficient as the wpf engine works directly with the xml.
If your going to do editing operations on the data, it is best to go with Approach two as you have the flexibility to manipulate the data far better. Using a class you can do the exact same thing as in approach one with binding as the engine will read from the objects just like the xml file. This approach allows for easily binding the elements 2 way and if its from an IObservable Collection, you can ovveride the method that is called when the data is changed, so it updates the xml.

Categories