VB Listview help (WPF)

Soldato
Joined
27 Sep 2004
Posts
11,201
Location
The Ledge Beyond The Edge
I have a Visual Basic 2008 WPF form and in it i have a list view. WIthin the list view i have text boxes so i can add items to it (in a master detail type set up).
How can i set the value of the text boxes automatically? Normally i would just have txtBox.text = Blah. But i can't do that for text boxes within the list view.

Helpppppp
 
Data binding would be the ideal route to go. Bind it to a property in your object or whatever, and bind it to it.

Depending on how large your project is and how experienced you are with WPF, consider using Model-View-ViewModel (MVVM), which is probably what the previous poster is referring to.
 
Don't bother. It's an unnecessary burden, especially if it's a small project. Concentrate on learning the fundamentals of WPF first. Data binding being one of the things you should get to grips with.
 
Cool, i will have a look into databinding. Is it the same as binding paths etc which is what i am using already.
Excuse the n00b questions, but will that work with all my data coming from LINQ?
Oh i am wanting it to map to a system item such as tme. Will that matter?
 
Last edited:
This is what i currently have in my xaml
Code:
<GridViewColumn Header="Updated Date/Time" Width="125">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=UpdatedDateTime}" 
                                         />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
 
Yeah, that's data binding. Yep, you can use data binding with LINQ, and LINQ to SQL, or however you're doing it. There's quite a lot of information on Google on data-binding, since it's one of those more important concepts of WPF.

Generally, what I do now is use the INotifyPropertyChanged interface with my business objects, and have properties use the OnPropertyChanged method when their Set has been called. If you're binding collections though, you might want to try ObservableCollection because it automatically updates the interface when itself gets updated. You may have come across Dependency Properties; these are quite similar to using INotifyPropertyChanged. Some people prefer them, I don't. Again, check Google for differences and why people use which.
 
Thanks for you help mate :)

I'll look into INotifyPropertyChanged to do it, because all i can see just now would change my current binding path to the date, and if i do that, it wont write it to anywhere on the DB. Can i do both on the binding path?
 
Hm? Can you write from the interface to the database...? Yes, data-binding can be TwoWay. Just like you do Path=<Blabla>, add on a "Mode=TwoWay"

From what I understand of your program, you have this ListView/GridView. What is this supposed to display exactly? A collection of objects that have a DateTime property? Or simply a collection of DateTime objects? And this collection is filled using LINQ? If so, you can convert your LINQ IEnumerable to an ObservableCollection quite easily and use the ObservableCollection. ObservableCollection has a CollectionChanged event that you can use to always update the database when a change occurs from the user interface.
 
What it is, is when i want to add a detail record on a master detail form, i want it to automatically populate the date field with the current date. In the master part of the form i would just do a txt.text=Now but i can't seem to name the text field in the listview (which does the detail lines)

I now have the code blow, but it doesn't do anything lol and i have this at the top of my XAML form
xmlns:sys="clr-namespace:System;assembly=mscorlib"

Code:
<GridViewColumn Header="Updated Date/Time" Width="125">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Source={x:Static sys:DateTime.Now}, Path=UpdatedDateTime}" x:Name="txtJohn"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
 
You shouldn't need to put it anywhere else. All the user interface is doing is displaying information to the user. That information is manipulated behind the scenes. Data binding allows you to manipulate the data any way you want to and always have it show correctly to the user.
 
Sorry i must be being really thick lol I am using VB.

But even when i set the
<TextBox Text="{Binding Source={x:Static sys:DateTime.Now}, Path=UpdatedDateTime}" x:Name="txtJohn"/>


It isn't putting in the date when i try to add a new line :(
 
No. Look at the link in my other post. Static properties like Now in DateTime can't be bound. But the author of that post sets out a method of getting around it.

edit: I apologise, I completely forgot youre using VB.NET. If you can translate from C# well enough, do that, if not try and find a similar post that uses VB.NET.
 
Last edited:
Back
Top Bottom