|
The product is changing. I added the textblocks in the 2nd and 3rd grid rows to show the values of the properties as the combobox selection is made. I also put a break in and "mySelectedCategory" is updated when a choice is made to product.
I made a change to TwoWay and saw no difference.
My understanding of the binding modes matches what you have listed but I thought that UpdateSourceTrigger was still needed to let the viewmodel know that an update was made from the source. With OneWay I do indeed want to say that updates are not sent back, but UpdateSourceTrigger is there to let the view know that updates were made to the model.
|
|
|
|
|
Ah, Simon nailed the problem...
The category being dumped into "mySelectedCategory" isn't IN the list. It's an object created in GetMyCategory() that has the same values as one of the items in the list.
So the solution is to override the Equals() function of the category, so it returns true when the ID and Name match (Or just the ID). You'll also be told to override GetHashCode (Also a good idea)...
That way, the data binding system will be able to match "mySelectedCategory" to an item in the list, and select it for you.
|
|
|
|
|
You were correct and I posted my solution in the other post. I think I see the issue.
Regarding the statement that the category wasn't "in the list", given the common example displayed here, am I missing a better way to do this?
My assumption is your data layer will have functions to get a list of objects (for maybe a combobox) and also have a function to grab an object by ID. Are you implying that maybe my data layer should be doing something different for it's "get by id" type function? I'm all for learning a better method.
|
|
|
|
|
Well the trick is that you're not returning an item from the list of categories... You're returning an entirely different item with the same properties, when you do this:
Public Function GetMyCategory(ByVal id As Int32) As myCategory
Dim results = From c In _mycateorylist
Where c.CategoryID = id
Select c
Dim rs As IEnumerable(Of myCategory) = From x In results.AsEnumerable
Select New myCategory With
{
.CategoryID = x.CategoryID,
.CategoryName = x.CategoryName
}
Return rs.FirstOrDefault
End Function
If you just return results.FirstOrDefault , it'll give you the first result from the actual list. You're also doing the same thing with GetMyCategories() , by calling it once in the data model's constructor, and again in the view model's constructor, yielding two different collections with identical contents. The idea is to have one collection in the data model, give the view model a reference to that collection, and let the GUI bind to the view model's reference, so they all point to the same data.
|
|
|
|
|
Have a look at this article
Debugging WPF data bindings[^]
Hopefully it can help you debug your binding.
A quick tip, as myCategory does not override equals then mySelectedCategory will only be displayed/selected in the combobox if myCategoryList contain the very same reference.
My number one dev tool? Google
|
|
|
|
|
In a previous version I had tried that but it appears I coded it incorrectly. For some reason I tried to implement IEqualityComparer and when I did nothing changed. Instead I tried an override to Equals and it seems to have worked.
Sigh. I had the answer from a Google search a while back, I just implemented it incorrectly.
Here's what I coded that appears to have worked.
Public Overrides Function Equals(obj As Object) As Boolean
If IsNothing(obj) OrElse Not [GetType]().Equals(obj.GetType()) Then
Return False
End If
Dim myc As myCategory = CType(obj, myCategory)
Return CategoryID = myc.CategoryID
End Function
Public Overrides Function GetHashCode() As Integer
Return CategoryID.GetHashCode ^ CategoryName.GetHashCode
End Function
|
|
|
|
|
At first I reference http://blogs.msdn.com/b/jaimer/archive/2009/11/04/introduction-to-wpf-4-multitouch.aspx # 9931447 to implement multi-pen, but in the running, I found a noticeable delay between the touch point and the track, and using CPU resouces is very big.
In inkcanvas in writing when there is no such single issue.inkcanvas's static handwriting and dynamic handwriting is separate. In multi-pen implementation , how can I implement this effect?
public class Window2Panel : Panel
{
private VisualCollection _col;
public Window2Panel()
{
_col = new VisualCollection(this);
}
protected override Visual GetVisualChild(int index)
{
return _col[index];
}
protected override int VisualChildrenCount
{
get
{
return _col.Count;
}
}
bool down = false;
CustomPen pen=null;
protected override void OnMouseDown(MouseButtonEventArgs e)
{
down = true;
pen = new CustomPen(e.GetPosition(this));
_col.Add(pen);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (down && pen!=null)
{
pen.AddPoint(e.GetPosition(this));
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
down = false;
pen = null;
}
}
public class CustomPen : DrawingVisual
{
private Stroke stroke;
public CustomPen(Point point):this(new StylusPoint(point.X, point.Y))
{
}
public CustomPen(StylusPoint point)
{
StylusPointCollection points = new StylusPointCollection();
points.Add(point);
stroke = new Stroke(points);
}
public void AddPoint(Point point)
{
this.AddPoint(new StylusPoint(point.X, point.Y));
}
public void AddPoint(StylusPoint point)
{
stroke.StylusPoints.Add(point);
this.Draw();
}
private void Draw()
{
DrawingContext dc = this.RenderOpen();
stroke.Draw(dc);
dc.Close();
}
}
|
|
|
|
|
Here are two examples can be run, using VS2008 and. Net FrameWork3.0。
https://skydrive.live.com/redir.aspx?cid=8f125a6da8d15aee&resid=8F125A6DA8D15AEE!103
Delay example, the longer the draw, between the mouse and lines will be a very significant delay (after the mouse to stop, delay disappeared), and CPU utilization much, mainly because of the mouse each take a sampling point, we need to re-draw the lines;Non-delayed example you will find that handwriting is very smooth, although take up memory, but not as good as the first clear, the key is not that obvious delay.
My aim is to achieve multi-pen, refers to more than one person can use the stylus on the touch pad or finger free writing, which requires hardware support. Currently there is no hardware problem, the problem is a multi-pen when the delay is not significant (due to hardware limitations, there will be some delay in itself).
Examples of the use of delayed implementation, to achieve multi-pen, but the process caused the delay with the hardware itself, and do not make people suffer; example if the non-delay multiple ways to achieve freedom of writing, then, is an ideal, but I can not find how to DynamicRenderer the RootVisual simultaneously display different handwriting method.
What should I do here? how achieve multi-pen? Are there other ways? Please help me!
|
|
|
|
|
Hi,
How to get WM_Gesture/touch messages form windows 7 in WPF 4.0 touch aware application?
Currently I am only able to get Gesture notify message but not actual WM gesture message. but same we can get in win Forms.
|
|
|
|
|
Hi,
I work with WPF, and I have got a problem with binding a listbox.
The question is as follows.
I have a left list box of ten list box items. On the right hand I have a border where I can exchange between 10 User Controls.
So, I have a 10 listbox items for ten usercontrols. The problem is how to bind these 10 listitems and make them as a menu, so when I click on one of them I get the right User Control on the right side..
If ther is any solution, send please!
|
|
|
|
|
Thanks for your contribution and your funny comment in the bottom!
I have done exactly what you have there up! But the problem is in the collection. Because I have different VM for different Views, I couldn't create an instance of a collection.
Even if I have done an Array list, it would be difficult to determine/create and index that would return the right UserControl!
|
|
|
|
|
I have VS2010. Of the dozen plus examples and papers on WPF I have tried I find all are out of date. Some only need to be tweaked a little. Some are useless. It seems like WPF has really been a work in progress. It is not the author’s fault that Microsoft keeps changing things so much. Not even Microsoft's since they are trying to improve things and have lots of competitive pressures. It is just plain frustrating.
Sometimes the author provides info on the VS and .NET versions but not always. It is really helpful when they do.
Is there a way that after we get an example working in new version we could posted the updated source code with the article
|
|
|
|
|
Well, you could try the book in the post below. You can download the code for the examples in C#/WPF4 for VS2010 - everything is up-to-date. Just a suggestion.
It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca
|
|
|
|
|
I have that book. I also have Pro WPF in VB 2010. I find both useful. Surprisingly I find little duplication between the two books. I guess that shows how really monstrous WPF is.
|
|
|
|
|
I don't think it has changed that much. Its just that they've filled in a lot of the holes over time. You aren't really going to find a good, basic example of WPF MVVM because people usually post the full blown sample apps and you get all confused. Plus, theres about 10 different MVVM frameworks out there, so the way you do things in one might be completely different or missing from another.
Those books are good for simple, isolated examples, but they aren't very good for full blown app examples.
|
|
|
|
|
I do not see any difference between WPF and VMMV. I know technically there is but from a practical matter I open Visual Studio, select the language, and WPF project. That is WPF; that is the default VMMV. To me they are the same. That has been a moving target.
Case in point, DataGrid was not in VS2008 but is in VS2010. There were several versions of DataGrid before that and in an SDK. I find comments that even different revs of SDK had slight changes in DataGrid. Now we have VS2010 and it is even different yet. A moving target. All examples of DataGrid not done in VS2010 are useless. There are lots more examples.
I really agree with you comment about isolated examples not being very useful. It seems every time I try to combined things it blows up in WPF. It did not do that in Windows Application in either C++ or VB. I started with VS version 4 VB. Did C++ for many years. When .Net came along I when back to VB. It was just as structured as C++ by then. I got tired of all the extra punctuation and lines and brackets. The examples worked well then. They seemed easy compared to WPF.
|
|
|
|
|
Not at all. Winforms is pretty much managed MFC. You do things in the same exact way. You subscribe to events, intertwine business and UI logic, make things public as workarounds, global variables, etc. You *can* write WPF like that just fine. That is not MVVM. MVVM introduces architectural design patterns. It seperates UI from business logic pretty much completely. Also, you are doing everything via data-binding. If you are doing full blown proper MVVM, you are also delving into lots of the more advanced concepts like Dependency Injection, Services, Service Locator, etc. With Visual Studio 2010 and .NET 4.0, you can't do MVVM "out of the box" because the infrastructure support is not there. You need to add a 3rd party MVVM library or write your own.
-- modified 29-Sep-11 15:56pm.
|
|
|
|
|
I read some articles writen by Microsoft people and I think they would dissagree with you. But, they think VS is the answer to all things.
|
|
|
|
|
How so? What exactly are you disagreeing with? WPF is not MVVM. MVVM is not WPF. Nor is MVVM supported out of the box with no additional libraries or code. Microsoft doesn't claim to support MVVM with Visual Studio 2010. They just recommend you write WPF & Silverlight code in that style. Where is RelayCommand<T>? Where is your ServiceLocator or DI container? Where is Messenger? Those are not part of .NET at this time. Why do you think there are 10+ MVVM frameworks out there? Cinch by Sasha Barber is a popular one, MVVMLight, etc. Heck... even Microsoft itself has a library called Prism, although thats not purely an MVVM library, thats more on the MEF side of things.
You will find REALLY quickly, that if you are doing MVVM, you need a lot of support code.
For a simple example:
How do you close a dialog from the ViewModel? Normally, you just set DialogResult = true right? Too bad DialogResult is not accessible from the ViewModel . Oh wait, guess what? Its not a bindable property either! So you can't bind it to something in your ViewModel. Guess you need some additional (3rd party) code to do that . If you google that, you'll find that what everybody is doing is using attached properties in a framework.
Another example where VS2010 does not support MVVM out of the box:
How does ViewModel1 communicate with ViewModel2? They aren't supposed to know about each other. Hmm... guess you need some sort of Messenger implementation for that. Too bad .NET 4.0 doesn't have one .
Yet another item:
How do you forward control events to the ViewModel? Thats not supported out of the box either... Darn... guess I need some additional generic code to do that .
|
|
|
|
|
I reread the article:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
I understand your "technical" view but my statements about a "practical mater" still stand.
Maybe my opinion comes from the type of programs I write. I do the glue. I start with two or more pieces of equipment and/or software that need to work together and do something the customer wants. They were not intended to work that way. I glue them together. I often name my programs DuckTape. Often it is just as much a UI as it is communications but always I am quickly in and out of a project.
I wonder how many people use a VMMV?? I doubt it is a majority.
|
|
|
|
|
Lol... pretty much everybody who writes WPF uses MVVM. Why do you keep calling it VMMV? WPF and MVVM go hand in hand. As I said, its not required, but its highly recommended by Microsoft.
If you are just hacking together quick apps, then yeah, MVVM is overkill. WPF is kind of overkill too. You could probably whip out a quick "DuckTape" app quicker with Winforms.
-- modified 29-Sep-11 18:30pm.
|
|
|
|
|
Winforms are quicker but I got board wanted to try something new. Also, you always need to keep learning.
VMMV. MVVM. Dyslexia I guess,
|
|
|
|
|
I see you've pointed to Josh Smiths excellent article on MVVM. FYI - Josh does not work for Microsoft. MVVM and WPF go hand in hand - in a large part, WPF was designed to be able to support MVVM. The problem is that people have it in for MVVM because they think that
a) you HAVE to use it for WPF; you don't, but it certainly makes it easier if you do
b) MVVM is complicated; it's no more complicated than other patterns
c) MVVM is all you'll need; it's not - MVVM is supported by a whole slew of other things such as IoC, DI, Service Location, Mediator/Messenger - all of which are well documented in their own right. More importantly, if you use another pattern such as MVC, you still end up having to use a lot of these techniques.
If you're knocking up a quick one off application for yourself, then MVVM is overkill. If you are intending to produce a professional application that has the model and UI interaction logic in a form that can be tested using good programming practice, then MVVM is perfect for you.
|
|
|
|
|
What I write works, customers are happy with it, they pay me a lot of money for it. What is not professional about that?
|
|
|
|
|
So you don't unit test your code?
|
|
|
|