I have a DevExpress dxe:TextEdit control with EditValueType="{x:Type system:Decimal}". I would like this TextEdit control to display and accept values that are 10,000 times the actual value. E.g., if someone enters 15, it should save the actual value as .0015. And if the value in the viewmodel is .0015, it should display 15.
I am new to WPF and I am not sure the best way to accomplish this. It seems like I can't do it with format strings, but I could be wrong. A value converter seems like overkill. I also thought about modifying the setter on my model object, but I don't think that would work, and it seems sort of hack-y.
I've worked with DevExpress a while ago.
As far as I can remember, you can create your own TextEdit class. (Derive from TextEditBase or even go further and derive from BaseEdit).
However, talking about over-killing, I think this is a much greater over-kill than just creating a converter, which will be a really good solution IMHO.
I would use the get/set functions to accomplish this. Perfect way of using them to translate presentation format to/from storage format.
Related
I am writing a c# winforms app that makes a couple of rest calls to a web service.
I have several textboxes with different behaviours. To take a specific example, one is called txtPrice. I have two buttons that increase or decrease the price across an (uneven) range of permissible values (btnIncrease and btnDecrease).
I need to parse the value in txtPrice to send it to the webservice. I have thought of three ways of doing this, and I would like some advice (or perhaps a pointer to look at a different way of doing it).
Have form-scope variables (private double price;) which are updated on txtPrice_Validated. I like this method as it separates the view from the model, but I dislike it because the data is duplicated. Also every time I update the price using the buttons I have to update it in two places.
Use member functions myForm.getPrice() and myForm.setPrice() which update txtPrice. Something about this seems ugly, storing data as strings, but it seems pragmatic.
Perhaps there is a kind of (non-database) entity I can bind to, which could encapsulate this logic?
More generally - is there a simple tutorial book which teaches a few basic exercises like this?
I need your opinion about UI and Databinding in WPF.
I had argue with my supervisor about UI & databinding. There is A class, it has ObservableCollection of B class, and B class also has ObservableCollection of C class. These classes are used while communicating with NI CAN and LIN device(it is not case).
On the other hand, in user interface there are bunches of TreeView and DataGrids. There are several instances of A class which is used to store data from devices and files, and they are binded to Treeview and Datagrids. But, he disagrees with this, and says to make separate other list of variables for TreeView and Datagrid to data binding. As he says UI data and other must be separate and I agree with it. But, in this case there are several lists of lists of lists and for example: copy from one list to other list when there is change on data takes much time and calculation ( -> UI element ). And other problems also.
I need your your opinion or other advice to come out better solution.
Thank you!!
It sounds like you are a student and doing this work as part of your studies. Either way, there is something to learn here.
Why not implement both approaches and see how they differ, and which you prefer? As it stands your question does not provide enough information for anyone else to tell you, and there is no way to short cut this kind of learning in my experience.
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.
I am trying to get multilingual translation("label or caption") string value for the given elementname in wpf.
For example; for elementname "txtDescription" ; my IValueConverter implementation will return with "Description" ; for another language will return different translation string(i.e. descripciĆ³n for Spanish) and the translation string will be Text=... of the same element.
I am new to wpf; I cant make it work. Is there any elegant way to do that with similiar manner as below.
<TextBlock Name="txtDescription" Text="{Binding Converter={StaticResource MultiLingualConverter} }"</TextBlock>
If this is not simple or requires more code then as an alternative sending "txtDescription" as an argument to MultiLingualConverter is acceptable but I dont now how to do that neither.
Definitely, you should build good localization support in your system, better then using converter for every string.
Until today, the best solution i have found and i almost always use it is this:
http://blogs.microsoft.co.il/tomershamam/2007/10/30/wpf-localization-on-the-fly-language-selection/
give it a try. good luck
You can pass the element name via the ConverterParameter property, this is an example of how it is used to pass a string to the converter.
However, WPF localization is more than returning different strings for different languages. You can read this article for more information and there is a Run Dialog Box example in the page to get you started.
Due to cyclical dependency restrictions you cannot reference a control inside itself or its descendants in the tree.
I would recommend you not to use converter for multilingual support. it is not a best way to do this.
got here for best way to do.
https://msdn.microsoft.com/en-us/library/ms745650(v=vs.110).aspx
No way to explain this issue except by example:
Say you have a custom UserControl with two DependencyPropertys, StatList Stats and string ImportantStat. The job of the user control is to display a table showing all of the values in Stats but with special visual treatment (like a pie chart) of the ImportantStat.
My instinct was to write a block of XAML that looked more or less like:
<PieChart Value="{Binding Path={Binding ImportantStat} }"/>
where the DataContext is prior set to Stats. So, the user passes in ImportantStat = "WinPercentage" and the pie chart binds to the WinPercentage Property of the stat line. But the user can just as easily pick some other Property to emphasize.
The problem (of course, you already know this, educated Stacker) is that you get an error message stating that you can't convert from Binding to string, which is what the outer Binding expects for Path. Though I haven't proven it to myself, I am guessing this is simply because Path is not a DependencyProperty.
So, is there any way to achieve my goal here? Feel free to break my assumptions in that first paragraph. Maybe, for example, ImportantStat can't be a string but must itself be a Binding.
Edit: Attempt #1 Failed
I was hoping that exposing from the code-behind a new DependencyProperty Binding ImportantStatBinding would allow me to rewrite the XAML as:
<PieChart Value="{Binding ImportantStatBinding, RelativeSource=... }"/>
...but to no avail. The indirect Binding is just stuck into Value itself with no attempts to resolve it.
My backup solution, which might be where this is headed, will be to just create the content inside the code-behind where I have access to ImportantStat directly and so can get away with a single Binding.
Far as I know, there is no way to concatenate data bindings in this way, without additional code. To put the problem more simply, we can have data binding (of course) of the form:
A --> B --> C
but you cannot have data binding of the form:
A --> B --> *A (*A indicates the target depends on the value of A)
because the relationships must be fixed.
It seems like it might be possible to create a Converter whose job is to convert a string into an arbitrary value by actually dereferencing a Binding using some additional context and that string as the property path. That sounds messy with type issues, so I chose the only other way I could think of:
I added a new DependencyProperty for the PieChart to the code behind and made sure that I constructed it at the appropriate times, so that the XAML could consume it. It's ugly, but it works. I just feel a little dead inside :) Hope someone finds this useful some day.