Wpf - DataContext Issues With ListBox After Changes To MainWindow.xaml?

Jun 18, 2012

I am new to WPF and the MVVM design pattern this month, and more than a little out of practice in general. To learn, I've been playing around with textboxes, rectangles, and ways to display them in a window. I began with Ashley Davis' excellent "Simple Drag Selection in WPF" tutorial, which walks through creating a view model for a collection of rectangles, binding a listbox to said collection, and styling the listbox with a canvas, creating a data template for the rectangles, as well as basic "rubber band" selection logic.

I have since built on this tutorial to improve the drag selection so that it behaves much more like selection does in windows explorer, and to allow resizing the rectangles from the corners or edges.All was well until I changed the MainWindow.xaml in an effort to include a column on the side for various buttons and controls, thus moving the "editor surface" grid from inside a 1x1 grid on the main window to a column of a 1x2 grid, moving the data template to the grid's resources (since it will be the only element in the window that needs it). As soon as I did this, subroutines that I wrote which interact with the listbox started misbehaving--rubber band selection no longer works. There's no visual indication that listbox items are being selected (they were highlighted previously), and interrogating listBox.SelectedItems.Count after a drag-selection mouseUp event returns a zero.

After some experimentation, reading many questions on this site and sections of my WPF Unleashed book, and going over the msdn databinding overview, as of this morning I still cannot find my mistake(s). I believe it is a data binding mistake or incorrect data context.Some details about the view models involved:

DataFieldViewModel

...implements INotifyPropertyChanged and exposes properties for its (in this case a rectangle and a textbox) X,Y position, width, height, visibility, and selection status (a way to track it across several rubber band selection operations)

PageViewModel

...implements INotifyPropertyChanged, and has among other things an ObservableCollection of type DataFieldViewModel, called DataFields, and exposes it as a ReadOnly Property.Here's a look at MainWindow.xaml.vb and one of the broken subs:

Namespace EditorUI
'
' The main window of the editor.

[code]....

View 1 Replies


ADVERTISEMENT

In A Datacontext Are Inserted Values Not Available Within Datacontext Until After Submitchanges

Mar 18, 2009

I'm going through an XML file of articles and the journalist(s) that wrote them. As we are adding the articles into _Data our datacontext we may come across a journalist that needs adding so we do this: [code] However subsequently we may come across this same journalist again and nothing is returned when we do this: [code] So it uses the code above again to insert the same journalist again.Once all of our inserts are done we do a submitchanges. At this point it has a head fit:INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_ articles_ journalists_journalists'. The conflict occurred in database 'blah', table 'journalists', column 'id'. The statement has been terminated.From looking through the sql generated in sql profiler you can see that it is trying to add some journalists more than once, this will fail as the name must be distinct. The subsequent records that are trying to be inserted with these journalists are failing as the journalist wasn't updated.Surely if I have a collection of journalists, add some to it and then look in my collection I should see all of them and not just the original ones.

View 3 Replies

Xaml Writer How To Remove Xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Sep 17, 2009

I need to use the xaml writer to convert in string some controls. What I get is a string like this for example

[code]...

View 1 Replies

Possible To Create VB .Net Apps Without A Mainwindow?

Jan 20, 2009

is it possible to create VB .Net apps without a mainwindow?I want to call my VB app from another app. It will just take a few files, and input them in a database. So, I need no window.

View 1 Replies

Wpf - Access Controls From MainWindow?

Feb 5, 2012

I have a text box in the MainWindow. I was wondering how I can access it from another window. I tried declaring an instance of the MainWindow but it doesn't work. Say I want to set the text in the MainWindow to something, in SecondWindow. How can I do that?

View 1 Replies

Editing The Mainwindow Ui From One Of The Module Files

Feb 2, 2011

I have a program that is getting pretty big and it is a pain to find everything through all the functions and classes. I am trying to break it up into other files based on their method. Some of these functions have calls to others in the main class. I changed most my functions from private to public to access this. I had problems calling certain code created windows so importing mainwindow helped that. My last problem is editing the mainwindow ui from one of the module files. I want to make sure im on the right page before i continue breaking it up. My only guess is that anything they updates the ui should be left on the main class.

View 2 Replies

Get Data From MenuItem.DataContext?

Dec 15, 2011

I have a context menu on my DataTemplete for a list box, and that menu will contain links to Favourite and Retweet the tweet (the menu item) But I don't know how to get the information about the Tweet in code. I am trying the following Line:

[Code]...

View 3 Replies

LINQ To SQL DataContext To XML With XSD Schema?

Dec 27, 2010

I have some data in my Linq.DataContext.I had succes in converting it to an XSD - Schema, using the following code:

Dim changeset As System.Data.Linq.ChangeSet = c.GetChangeSet()
Dim objDic As New Dictionary(Of System.Type, List(Of Object))
If Not changeset Is Nothing AndAlso Not changeset.Inserts Is Nothing AndAlso Not

[code].....

View 1 Replies

Overriding Datacontext For View?

Mar 15, 2012

I am setting my viewmodel as datacontext in my xaml but I override it to my view to make few functions work however to achieve the visibility on some grids and I have a property in my VM can I override my datacontext back to my VM? If so how? I have a stackpanel that has datacontext overriden as my grid and within that stackpanel I need to change the datacontext for a button.

View 1 Replies

DataContext - Discard Changes Of Only One Record In Table

Apr 1, 2010

I would like to know if its possible to discard changes of only one record of only one table in datacontext. I use databind to bind my controls on a form. I modify one record at a time. after the modification, the user have to hit save button to validate. But he can hit cancel. I would like that the cancel button discard all the changes that the user has done. Is it possible?

View 3 Replies

Difference Between Using DataContext Class And SqlConnection?

Mar 2, 2012

I asked a question earlier where I was told a simple way to "bind data to objects" is to just run a SqlConnection(connectionString). The response also included a comment saying I could get fancy with L2S and Entity Frameworks, so I looked deeper into those. It seems all you have to do with the DataContext object is point to the database. Why would SqlConnection be a benefit? What is the difference (or pros/cons) of using either one of these? Is one more "standard"? Is one more modern?

View 1 Replies

Two Way Binding LINQ To SQL DataContext Classes

Aug 17, 2009

How to bind a textbox in design view to a LINQ to SQL datacontext class?

View 3 Replies

Understanding ItemsSource And DataContext In A DataGrid?

Jun 14, 2010

UPDATE: See the bottom of this question for what I did to solve the problem.I'm trying to understand how the ItemsSource and DataContext properties work in a Silverlight Toolkit DataGrid. I'm currently working with dummy data and trying to get the data in the DataGrid to update when the value of a combo box changes.

View 2 Replies

Make A Single Instance App And Showing The MainWindow When Another Instance Is Launched In .NET With WPF?

Aug 30, 2011

I am looking for a way to make my app running in a single instance mode and showing the MainWindow of the first instance when another instance is launched.I do a quick search on the internet but I did'nt find anything to open the MainWindow of the first instance or it was for Windows Form not for WPF.

View 1 Replies

Check Whether A Linq-to-SQL Object Is Already Attached To A DataContext?

Jun 27, 2011

I have an object that may have been inflated via a plain old DataContext, or may just have been new-ed up with just its .ID property set. There's no way to know for sure. I'm looking to rehydrate the entire object from whatever is in the database. If the object was new-ed up, I can call .Attach() on the table object and refresh from the Data Context with no trouble. But, if the object was already inflated from the DataContext I get the error: "Cannot attach an entity that already exists.". There's no timestamp field or anything like that - just an integer primary key being used to control the rehydration. I'd like to know if there's a way to conditionally attach. Here's the code - it works the way I want it to, but this seems a hackish way to go about it:

' myDC is a shared instance of a vanilla DataContext...
' myObj is an instance of a linqed-up `SomeLinqObject`
Dim tbl = myDC.GetTable(Of SomeLinqObject)()

[Code[,,,,,

View 1 Replies

Setting The DataContext On An Entity Instance After It's Retrieved?

Apr 14, 2011

I'm trying to find a way to have the DataContext available from within Entities.

I want to do something like this:

partial public class MyEntity
public DataContext as MyDataContext
private sub OnLoaded()

[Code]....

View 2 Replies

Setting User Control's DataContext From Code-Behind?

Jul 8, 2009

This should be pretty easy, but it throws VS2008 for a serious loop.I'm trying out WPF with MVVM, and am a total newbie at it although I've been developing for about 15 years, and have a comp. sci. degree. At the current client, I am required to use VB.Net. I have renamed my own variables and removed some distractions in the code below, so please forgive me if it's not 100% syntactically perfect! You probably don't really need the code to understand the question, but I'm including it in cas.

I have a very simple MainView.xaml file:
<Window x:Class="MyApp.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

[code].....

View 3 Replies

Using Same Connection String In A DataContext Constructor And SqlConnection?

Oct 4, 2011

I am preparing to deploy a web service that uses SqlConnection primarily as its means to get to the database, and I am adding some new methods that use a DataContext instead of thatand the default constructor and DBML file would use a connection string refering to my development machine (I believe...)

View 1 Replies

DataBinding LINQ DataContext - Add A New Record Since The Textbox's Are Binded To An ISingleResult?

Aug 13, 2009

I have a LINQ to SQL DataContext with all my tables and all my stored procedures in it. Let me first start off by saying that we are upgrading our project from an access project to a WPF project. So in the Access project you could set a forms datasource to a stored procedure and edit and add new information. So in my WPF project on the page load I call my stored procedure to return an ISingleResult and then call a function to set the binding on every single textbox on the form. One is there an easier way to do this and two what if I want to add a new record since the textbox's are binded to an ISingleResult?

View 2 Replies

Using The Xaml.cs Or Xaml.vb?

Jan 8, 2012

I have been watching "absolute video tutorial". In those tutorials when doubled clicked on stuffs like "button" or "textbox", a code page appears named as "mainpage.xaml.cs" but when I use Visual Basics and do the same, on double click a page "mainpage.xaml.vb" appears. In this the coding stuffs are different. And when I write the same codes like in tutorial I get errors.

View 6 Replies

My.Resources In WPF XAML?

Sep 14, 2009

Is there a way to access My.Resources thru Xaml?

Like this

<Image Source="{Binding MyImage,
Source={x:Static my:Resources},
Converter={StaticResource MyBitmapToImageSourceConverter}}" />

[Code]....

View 2 Replies

Animate Gridlength In XAML?

Jul 27, 2009

I was just wondering if it's possible to animate the height of a grid using purely XAML? I looked at this tutorial[url]...

View 2 Replies

C# - WPF: Binding TreeView In XAML

Dec 8, 2010

I have a data source:

[Code]....

View 2 Replies

Wpf - Bind More Than One ObservableCollection In XAML

Feb 4, 2012

How can I implement both of these ObservableCollections in my xaml? I want to have a listview with all my students, and another listview with all my teachers.For now I just made an example student and teacher object. Am I going about this the wrong way. Eventually I will pulling from a datatable, not sure how I would implement it to the ObservableCollection though.

Public Class PersonalViewModel
Public Sub New()
Dim obcollection1 As New ObservableCollection(Of Student)

[Code].....

View 1 Replies

Wpf - Programmatically Set Height Of Row In XAML?

Jul 26, 2009

I'm doing a WPF project and decided to try it out with Visual Basic. So, basically I'm trying to set the height of a row in a Grid. I'm not great at programming but with C# I managed somehow to figure it out, without remembering how. It actually looks like an animation because when clicking the button, the height of one row smoothly goes down to 0. But I'm trying to even reach that property and I can't figure out the context. The row is called "AnimationRow" and I can reach that, but where do I set the height?

View 1 Replies

Wpf - XAML Code For TREEVIEW?

May 14, 2011

Following is the code I wrote for generating treeview hierarchy,

For Each k As KeyValuePair(Of String, GenreSet) In GenreSetDictionary
Dim t As New TreeNodeSet
t.Genre = True

[code]....

View 1 Replies

WPF And XAML Free Tutorials

Oct 7, 2009

I am new to WPF and XAML. I want to learn these for use in VB express 2008.Please guide me for some tutorials to start and progress in WPF and XAML.

View 3 Replies

.net - Accessing Class Properties In Xaml?

Feb 18, 2010

I've currently created the class below. For some reason though I can't access the properties I've created through my xaml style.

Public Class Ribbon : Inherits Button
Private mpopDropdown As Popup
Public Property Dropdown() As Popup

[Code].....

but they don't seem to expose the property either. I've also tagged the property as <Bindable(True)> but that didn't seem to do anything.

View 2 Replies

.net - Xaml TextBlock Width Inconsistent ?

Jan 18, 2012

Look at this simple bug :

<textblock (...) Width="0" />

Working great, the textBox appears to be 0px thin. Works with value like, 2, 3, 4...

Now that is NOT working :

<TextBlock (...) />
<TextBlock.Width>
<MultiBinding Converter="{StaticResource WidthConverter}">

[CODE]..................

Why can i set zero value to TextBox.Width in xaml and not in code behind ? By using a converter, when returing 0, the TextBlock.Width is not set to 0 but to "auto", i can read the text

View 1 Replies

Bind ObservableCollection To A DataObjectProvider In XAML?

Sep 14, 2011

I want to bind data to an objectdataprovider.[code]...

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved