|
I have data I need to process with record times at nanosecond resolution (specifically, measured in nanoseconds since the start of the year the data was recorded in, stored as uint64). For now I just need to turn it into a human-readable date and time, like 2012 May 28 11:35:58.000045921 (edit: adjustments will need to happen later, but that's not an immediate requirement). I figured at first (being a .NET and C# newbie) that DateTime would be able to do it with some finagling, but I guess from google that it only can store down to the microsecond, chopping off the 921 nanos at the end there. (Apparently they're important. ) (edit: or is it 0.1 microsecond? Anyway still two digits short in that case) Do I have any options short of, say, making the DateTime with the microsecond precision out of the uint64 nanos, ToString()ing it, and just tacking nanos % 1000 to the end of the string? Strikes me as maddeningly clunky, even if it'd work for now. Ciaphas fucked around with this message at 00:25 on Jul 12, 2014 |
# ¿ Jul 12, 2014 00:17 |
|
|
# ¿ May 2, 2024 06:02 |
|
Bognar posted:DateTime precision only goes down to the 100 nanoseconds. If you just want to use the built-in formatting DateTime formatting options, then I would say to make a class that wraps DateTime (e.g. PreciseDateTime) to delegate .ToString() calls and use whatever level of precision you need. Thanks, I had a feeling it was gonna come down to that but I just wanted to make sure. I guess overriding all the calls I care about for actually manipulating those times won't be too awful. drat sight better than fuckin' struct tm and time_t horsehockey, anyway. Going from C/C++ for over a decade to learning how to C#/.NET is kind of eye-opening.
|
# ¿ Jul 14, 2014 02:31 |
|
I have a List<DataRecord> dataRecords that I want to display in a WPF DataGrid. DataRecord is defined more or less as follows:C# code:
Setting my DataGrid's ItemSource to dataRecords does more or less what I expected: give me two columns of Yeartime and Items, the former being the numbers I expect and the latter being "(Collection)". I want to programmatically break out that List<DataItem> each into their own column, with the header being item.Info.Name and the cell data being item.DataAsString. How would I go about this? Binding is a confusing and horrifying subject to me, I'm just barely getting started with C# and .NET let alone WPF Ciaphas fucked around with this message at 21:01 on Jul 16, 2014 |
# ¿ Jul 16, 2014 20:59 |
|
Ithaqua posted:You can either define a template column to display the additional stuff, or let part of your viewmodel's behavior be to transform and project the data into a type intended for display. I ended up (after a lot of fuss and bother) going for Option 2, sort of. I turned off autogeneration in the datagrid, made its ItemsSource my list of DataRecords, and, in my xaml code-behind, generated, bound, and added the individual columns by selecting out the name and numeric index of each DataItem. I don't have the code in front of me but it was something like this. C# code:
|
# ¿ Jul 17, 2014 03:30 |
|
Yeah, I sort of realized that wasn't quite the Option Two being presented after I posted. I'm new to the whole MVVM thing too though--in fact this is literally my first go at making GUI anything since Visual Basic 6.0 in high school--so it took me a while to realize. I tried for several hours today to get binding to work in the XAML, but I kept getting blindsided mostly by the fact that the number of Items in each DataRecord, though the same across records, is completely unknown until runtime. (Their types are unknown, too, but that's what that DataAsString property is for ) What can I do in the XAML to set up those column definitions without that knowledge? (The fact that my data is essentially a list of lists with some metadata leaves me confused too, but I'm sure the solution to that problem is simpler.) Ciaphas fucked around with this message at 05:02 on Jul 17, 2014 |
# ¿ Jul 17, 2014 05:00 |
|
Ithaqua posted:Yeah, the Option #2 I was talking about was more along these lines: Thanks, I'll look up Automapper. This explanation kind of makes more sense with what I understand about MVVM anyway. Mr. Crow posted:You never really explained why Ithaqua's example failed you? It should have worked generally as-is; the second column wouldn't look exactly like you want, but nothing some header styles and templates can't fix. Yesterday it displayed absolutely nothing so I abandoned it, but I tried it again this morning and it worked . I must have typoed the first ItemsSource or something and not noticed. As you say, it looks like arse, but it does work, and I can probably figure out how to mess with those data templates. I'm going to try the second option as Ithaqua described it first, since that reorganizing will help me later anyway. (Context, I'm trying to make a graphical diff application for the files we're working with. Up to now we've used a utility to convert them to CSV and throw those into Beyond Compare, which works but has a few important limitations that make it a pain in the rear end. So I figured I'd cut my teeth on learning C#, .NET, MVVM, and WPF all at once by putting this together. I may have overreached myself ) (edit) Oh, AutoMapper is third party? That's out then, my work machine has no internet access (don't ask). I'm a little stumped at the moment at how to go about it manually. If I'm understanding things, I'd want to flatten what I've got above into something like this, right? C# code:
I'm sure I'm being stupid and there's a simpler way to go about it Ciaphas fucked around with this message at 19:09 on Jul 17, 2014 |
# ¿ Jul 17, 2014 18:58 |
|
Following up on my previous problem, once a friend told me about System.Data.DataTable, reorganizing the data became more logical in my head. Ended up being something like this code in my viewmodel: (again, typed from memory so probably wrong)C# code:
Made more sense to me than messing around with column templates, and seems more idiomatically correct than dynamically adding columns to the datagrid in the code-behind. It worked, but am I more or less on the right track here with respect to doing it "right"?
|
# ¿ Jul 18, 2014 20:09 |
|
In the diff application I'm trying to write in WPF, I have two bool arrays, RowDifferent[RowCount] and ColumnDifferent[ColumnCount]. In the DataGrids displaying the data I'm diffing, I want the following style: - If RowDifferent[cell's row number] XOR ColumnDifferent[cell's column number], color background light pink - Else If RowDifferent[cell's row number] AND ColumnDifferent[cell's column number], color background deep pink I know I have to use a style datatrigger, somehow, but I'm not really sure how to bind the trigger to an array of data. Any thoughts? (Sorry I can't paste the XAML here, work computer's not on the internet. )
|
# ¿ Jul 29, 2014 18:12 |
|
Is it possible to have a WPF application, depending on certain command line options being present, not bother with the Windows part and just run in the current console (or a new one if run from a shortcut)? I have a project that a customer wants to add some scheduled processing to.
|
# ¿ Aug 20, 2014 16:10 |
|
Are there no chart/graph/etc. controls built in to .NET/WPF by default? I've seen downloadable controls but I can't use those on my work machine.
|
# ¿ Aug 21, 2014 21:50 |
|
Anyone have any recommendations for WPF charting/plot/graphing controls that are free (and preferably open source)? I've tried Oxyplot already; while it basically works, trying to make my lineplot zoom to the actual data automatically when it changes is making me tear my hair out. (I feel like parts weren't made with MVVM in mind, 'cos I can do it fine if I cheat and access my view from the view model.) Ciaphas fucked around with this message at 00:33 on Aug 27, 2014 |
# ¿ Aug 27, 2014 00:30 |
|
Since we're talking about MVVM and WPF and how the rules work, let me ask a question about breaking said rules. I don't have my code from work, as usual, but is something like the stuff bordered by slashes a total faux pas? A loose coupling from the view-model to the view? (The specific actual code involves OxyPlot being, unless I really missed something, a bit dumb about trying to automatically pan and zoom to updated data for its plots.) C# code:
Ciaphas fucked around with this message at 06:17 on Aug 27, 2014 |
# ¿ Aug 27, 2014 06:11 |
|
Cool, thanks for the input. I'm still trying to learn WPF and MVVM, so knowing when and why I can/should break the rules is a big help.Gul Banana posted:Why not just have the viewmodel set a property RequiresUpdate and have a DataTrigger bound to that? I forgot about DataTriggers . I'd still have to have code in the CodeBehind to call MyOxyPlot.ResetAllAxes() or whatever it is, but at least there'd be less plumbing. I'll try that at work today, thanks!
|
# ¿ Aug 27, 2014 16:16 |
|
Am I the only one who thinks chaining dot-notation function calls is a gently caress of a lot easier to read? Or is that my C++ background showing? C# code:
|
# ¿ Aug 29, 2014 04:39 |
|
Assuming the resulting IEnumerable or whatever is the same, the order of your fluent-syntax calls doesn't have an effect on performance, does it? Like doing the .OrderBy() before or after the .Select() or whatever?
|
# ¿ Aug 29, 2014 16:37 |
|
Ithaqua posted:At least with LINQ to Objects, the answer is "of course it makes a difference." I can't speak to the SQL generated by LINQ to Entities. It's usually fairly smart about that kind of thing, but it's safer to assume it's dumb. That was kind of what I figured, but I wanted to be sure. So much of C# and .NET seems like frank witchcraft to me still
|
# ¿ Aug 29, 2014 17:13 |
|
Trying to relearn WPF and MVVM, and binding is still confusing me a bit. I have an enum called Status in my view model, with possible values of READY and OPENING (and more later). Instead of those, I want to display pretty values in my application's status bar, like "Ready." and "Opening (45%)", for example. Is the way forward to bind a label to the enum normally, write a StatusValueConverter with the strings coded in, and set the label's converter to it? Or is there a better way? Ideally, that 45% would come from the OpenProgress property in my view model. (edit) Sorry I can't post any code, separate computers for 'net access here. Ciaphas fucked around with this message at 00:11 on Apr 23, 2015 |
# ¿ Apr 23, 2015 00:07 |
|
GoodCleanFun posted:Use a property, maybe StatusLabel, that returns a string based on the value of the Status property in a case statement and your OpenProgress property. In the setter for Status, make a call to OnPropertyChanged for StatusLabel. Bah, that's a lot easier . Here's me always bloody overthinking it. Thanks lots.
|
# ¿ Apr 23, 2015 00:32 |
|
Asynchrony is confusing me a bit, was hoping I could get some help. My program has a POD class RawRecord. My viewmodel has private List<RawRecord> RawRecords that is populated by a member function private void PopulateRawRecords(), natch, which is called when the viewmodel's FileName property changes. PopulateRawRecords() takes about a minute on the test file I'm using which, as is, blocks the UI. I figured, okay, make it async and call it normally, I don't need to wait on the resulting List<RawRecord> anyway (the UI will just show me the ones that are loaded already along with a progress bar), and this is a good way to learn about async/await. Problem is, PopulateRawRecords() doesn't have any particularly useful points for me to put an await in, so it just runs synchronously anyway. Question is, what's the idiomatic way to handle this sort of background task when I can't await on any single asynchronous method? Rewrite it so I can? (Also, if it makes a difference, I'd like to be able to cancel the thing mid-load, but if I understand Task and TokenCancellationSource correctly that doesn't look too hard anyway.) (Also also, sorry again for no source code. Separate 'net computer again. ) Ciaphas fucked around with this message at 17:54 on Apr 23, 2015 |
# ¿ Apr 23, 2015 17:51 |
|
Bognar posted:More or less, yes, assuming that the time spent is in IO-heavy work and not CPU heavy work. If it's CPU heavy, then going async isn't going to do anything terribly special for you. You said you're working with a file, so there's an obvious place to use async/await, but I assume you're doing more stuff than just that. Ah, Task.Run looks like what I was looking for; thanks loads for that. As for whether it's IO or CPU bound, does VS2012 have any useful profiling to work that out or is it kind of guess and check? I can't copy the code here, but here's the overall flow: C# code:
|
# ¿ Apr 23, 2015 18:55 |
|
Inverness posted:Just at a glance you reading records from a file like that would be IO bound. Inverness posted:
I had tried ObservableCollection<RawRecord> yesterday for that very reason, in fact, but I couldn't then figure out how to, say, bind to RawRecords.Count (or is it size? I forget). Thought it'd be something like XML code:
(edit) Bognar posted:Ah, yeah, BinaryReader doesn't expose any async methods so you won't get any benefit out of making your method async. Stick with Task.Run. On a side note, you probably don't need to call RaisePropertyChanged inside your loop, you could just call it once on the outside. Depending on your UI framework, that may or may not have a performance hit. I call it inside the loop so that the user can view already-loaded records while the rest are loading. If it's a performance problem I can have it called every 100 loops or something, I suppose. Ciaphas fucked around with this message at 19:30 on Apr 23, 2015 |
# ¿ Apr 23, 2015 19:17 |
|
New question, just to make sure I've got the point. Is there any difference between binding to an ObservableCollection<T>, and binding to a List<T> that has RaisePropertyChanged() called for it on every add/delete? (Coming from another direction, there's no reason to call RaisePropertyChanged() for an ObservableCollection<T>, right?) Ciaphas fucked around with this message at 00:14 on Apr 24, 2015 |
# ¿ Apr 24, 2015 00:11 |
|
Ithaqua posted:This is a general OO thing. If it's not in a method, it's a field. A field a single instance of an object that is available to all methods within the class. In this particular case (and not knowing dick about ASP DBContext, mind) I'd guess that a new DBContext implies a new database connection, which you probably don't want for every call. (At least if it's anything like our Oracle DBs. Connecting takes about five million years, argh argh argh.) Inverness posted:If I wanted to bind to count I'd make a property that updated whenever the collection changed. Thanks for all this. I went back to ObservableCollection<RawRecord> today, and strangely the RawRecords.Count bind worked this time. Don't ask me why, I must have typoed hardcore the first time. At any rate this allowed me to take out a bunch of plumbing and cruft, so thanks for that!
|
# ¿ Apr 25, 2015 00:02 |
|
Is there a "correct" way to bind a text control's content to non-string data in one's model? My solution was something like this (working from memory, again...):C# code:
XML code:
Ciaphas fucked around with this message at 22:43 on Apr 30, 2015 |
# ¿ Apr 30, 2015 22:41 |
|
Bognar posted:This is the classic use case for a Value Converter. Thanks, I'll give it a proper look later. Seems like a lot more faff than just doing what I did, at least at first glance, though.
|
# ¿ May 1, 2015 00:46 |
|
Ciaphas posted:Thanks, I'll give it a proper look later. Seems like a lot more faff than just doing what I did, at least at first glance, though. Quoting myself because now that I've done this, I see the point. Ran into a problem, though. Here's something like my code: XML code:
C# code:
Separate question. My ViewModel has an ObservableCollection<RawRecord> RawRecords. One of RawRecord's properties is Category; in my UI I want to display a distinct list of Categories that are present in the collection. I can easily work out that result into a List<int>--something like RawRecords.Select(x=>x.Category).Distinct().ToList(), I think--but I have no real idea how to bind to that. Is it possible, or am I going to have to create a separate ObservableCollection containing the list of categories I've found?
|
# ¿ May 1, 2015 20:49 |
|
Is there a "right" way to open new views in MVVM? In a possibly ill-advised attempt to avoid controlling views from a viewmodel directly, I cobbled together something like this. Is it kind of the right way to go about it?XML code:
C# code:
Ciaphas fucked around with this message at 20:59 on May 7, 2015 |
# ¿ May 7, 2015 20:56 |
|
GoodCleanFun posted:See my post here for a very clean approach: http://forums.somethingawful.com/showthread.php?threadid=3644791&pagenumber=61&perpage=40#post442712636 crashdome posted:I do something similar. I call mine WpfWindowManager and forego the interface as its explicitly for use only with Wpf views. Although you could try and make an interface to encapsulate different platforms if you want. Thank you both, I'll give GoodCleanFun's code a lookover and work it out from there.
|
# ¿ May 8, 2015 01:12 |
|
With a WPF RelayCommand/DelegateCommand/whatever people are calling it these days, if I have a command that needs to be used across multiple view models, what's the "right" way to share that code out? A separate class inheriting from RelayCommand, then just instantiate & bind to one of those in each viewmodel, or is there some other method? (edit: does the correct way to do things change if the command in question needs to act on the calling viewmodel in some way?)C# code:
Ciaphas fucked around with this message at 19:17 on Jul 24, 2015 |
# ¿ Jul 24, 2015 19:11 |
|
While I'm in here, can anyone recommend a book on using MVVM in WPF, or MVVM in general? Going downstairs to the internet machine every five minutes with a new question at work is getting very old, very fast.
|
# ¿ Jul 24, 2015 22:19 |
|
crashdome posted:Do you work in North Korea? I get this question and its variants a lot, I was just trying to explain why googling my answers isn't working. I should really just stop mentioning the hosed up situation re: 'net access at work Ciaphas fucked around with this message at 22:48 on Jul 24, 2015 |
# ¿ Jul 24, 2015 22:42 |
|
GoodCleanFun posted:I would just implement the command in a base class that inherits from your ViewModelBase and then have both classes inherit from your new class. That way you retain all your members of ViewModelBase. Thanks a lot, that makes quite a bit more sense than inheriting from RelayCommand was turning out to. (One of the commands I have to share is, in fact, a Cancel/Close so that's really handy ) Ciaphas fucked around with this message at 22:48 on Jul 24, 2015 |
# ¿ Jul 24, 2015 22:45 |
|
I'm going nuts trying to sort this out in my head. In the MVVM pattern, are ViewModels expected to contain instances of their associated models, or should the actual data be stored/kept somewhere else? If the latter, what the hell should I be googling for? The first is what I defaulted to trying to learn this, but then I wondered how the hell two viewmodels should be sharing data that they both need to work with. (I sort of alluded to this earlier with the Command question. Really dealing with having multiple viewmodels in general is what's killing me I think )
|
# ¿ Jul 26, 2015 19:56 |
|
I'm trying to avoid using outside toolkits or libraries, at least at work, because of the Internet thing (I could download it and drag a CD upstairs but ). Is a good inter-VM messaging scheme much of a pain to write? While I'm at home I'll take a look at MVVM Light, thanks Ciaphas fucked around with this message at 22:51 on Jul 26, 2015 |
# ¿ Jul 26, 2015 22:47 |
|
Bognar posted:The concept isn't complicated, but there can be a lot of weirdness with garbage collection so you have to use WeakReference. Also, if you want your message subscriptions strongly typed (you do), then you have to deal with collections containing multiple generic types. It's annoying enough that you're probably better off picking up someone else's battle tested implementation. Sounds like is in favor of getting MVVMLight or some other library, then. Thanks.
|
# ¿ Jul 27, 2015 05:10 |
|
Coworker and I are having a minor disagreement on where to implement a few windows events on a ListView--specifically, DragOver, Drop, and KeyPressed (delete key, specifically, for removing entries). Right now he has them implemented as markup extension commands, instead of normal code-behind events; this means they (and their business logic) can go straight to the viewmodel. I feel that, because they use things like the DragOverEventArgs as the CommandParameter, which are inherently UI/view related, they have no business going to the VM at all--at least, not before parsing those event args into a list of files that were dropped, in this case--and should have been normal code behind events, despite the usual thing about code-behind being bad or whatever. What do you think? Either way works, we're both just still learning the 'right' way to do things in this cockamamie MVVM thing. (Sorry I can't post any code. )
|
# ¿ Jul 30, 2015 14:41 |
|
Ithaqua posted:Here's my guideline: Does the behavior you're implementing interact with the underlying data in any way? For example, if the "drop" interaction results in data moving from one observable collection to another, then that's not purely view related. It's the view manipulating the viewmodel via UI interactions, same as typing text into a textbox updates the viewmodel. For production, no, the view and viewmodel in question are proverbially joined at the hip. My only thinking was that, if I dig the Windows-y UI poo poo (DragEventArgs in this case) out of the viewmodel and leave it in the view's code-behind, we can attach a different view later--say, a console for headless unit tests. (And this viewmodel in question is one that does very much need to be tested.) Plus, neither of us really know what we're doing when it comes to MVVM yet; going against dogma when we don't quite know what the dogma is is kinda hard. What's the old saw, the rules are there to make you at least think when you're breaking them? Yeah, I don't know the rules yet, hence these posts The specific use case here is that drag-dropping a file should attempt to open it, adding it to the VM's ObservableCollection<Request> and thus displaying the dropped file in the ListView it was dropped on via binding. This was already implemented as a command leading to a method in the VM via a plain old button. So my figuring was, I'd move the actual drop event to the code behind, have it extract the file name, then call the VM's Open method directly (via something like (DataContext as MyViewModel).Open(fileName)). Ciaphas fucked around with this message at 16:18 on Jul 30, 2015 |
# ¿ Jul 30, 2015 16:08 |
|
Thanks to both of you. My coworker and I will discuss what you said when we meet again today. Separate question entirely I just remembered, and one a hell of a lot more general/easier to answer (I hope). Is this sufficient for overriding Equals(), or am I missing a piece? (I may have ballsed up the syntax--again, different machine) C# code:
Ciaphas fucked around with this message at 16:25 on Jul 30, 2015 |
# ¿ Jul 30, 2015 16:22 |
|
Sedro posted:You need to implement GetHashCode, and yours will throw exceptions when comparing to null. Sorry, what's R#? Google's coming up with some statistical programming language, which given context I don't think is right
|
# ¿ Jul 30, 2015 17:49 |
|
|
# ¿ May 2, 2024 06:02 |
|
ReSharper, I vaguely remember the dev team I'm helping was working on getting that, I think. I'll have to look at what it is, I've never heard of it, but if it helps me deal with bullshit boilerplate like that Equals stuff (thanks for that by the way) I'm all for it
|
# ¿ Jul 30, 2015 17:54 |