So I've added a bunch of items to a ListView. All of the items added are structures that have 4 members. The members are Amount, Location, Date_Time, and Category. These members are setup as properties and I was trying to bind each member to a column in the ListView.
Unfortunately, my results are blank entries or the added structures ".tostring()" return value. Essentially I have no Idea how to correctly setup up the binding. Any ideas in XAML or C# would be appreciated. Also the structure resides in a separate class, not the MainWindow class that contains all the controls. Formatting and styling is still in the works for my listview so don't judge my bad color scheme.
Thanks
Don't use images to show us your code. StackOverflow allows to insert code snippets. Also use paragraphs to format your question. Always see on your question after you've posted it.
Binding works only with public properties. Change access modifier of your structure and its properties from internal to public. See answer at this topic.
Related
I'm currently working on a WinForms graphical interface in C#. The form has a ComboBox that is displaying the contents of a custom class I wrote. This class implements the IList interface so that it can be used as the DataSource for that ComboBox.
The issue is that when I add or remove items to/from the underlying object, the ComboBox does not visually reflect this.
I have tried reassigning the list to the DataSource property whenever the contents change, but that doesn't seem to make a difference.
Most of the answers on this site that deal with this issue or similar seem to require a number of extra classes, or editing XAML documents, in order to achieve this.
Is there a simpler method to achieving this? If it makes a difference, the ComboBox will not be visible at the point where changes are made to the IList sourcing it. The only idea I've had so far is to possibly delete the ComboBox and replace it with a new one when needed, but I'm not sure if this is possible to do at runtime, and is obviously not very elegant either way.
I have a list of objects that is being used as the source for a DataGrid. This list of objects is a base class type, of which there are 2 or more inherited types. I am trying to bind to properties of the base class to display as values in the columns of the DataGrid. The subclasses have different properties available to them which I would like to be able to display, so my question is, does XAML have a way to dynamically change the value of a binding based on the type that is being pointed to? I have thought about potentially doing this with a converter, but if I understand correctly, then I would have to write a different converter for each subclass property that I need to bind to. Any advice or suggestions are appreciated. Thanks
This is probably possible, with attributes and some reflection you could mark the properties that should go in the various columns and then auto-generate those. You could also create a sparse grid, with columns for all possible properties; this should also be possible via reflection and does not require any additional metadata.
(You can auto-create the columns based on the items using an attached property (like this) if the native DataGrid event is not sufficient for this.)
You can define multiple DataTemplates and specify the intended type through the DataType property. The correct template will be selected depending on the type of the bound object.
https://msdn.microsoft.com/en-us/library/system.windows.datatemplate.datatype(v=vs.110).aspx
The means to achieve this would be a CellTemplateSelector.
See this link.
I want to be able to bind a listbox, Treeview or some control to a ObservableCollaction>SomeObject>() and then auto generate textboxes to all properties with a public set within SomeObject. That way I dont have to add or remove any code in WPF if someone remove or add a new property to SomeObject.
Can anyone please point me in the right direction or tell me if this is a stupid idea?
//Max
I don't think you should use ObservableCollection for that. That's used when you want to present a collection that will be modified in runtime (i.e. having elements added, removed or modified). Do you expect SomeObject properties names to be modified at runtime?
If you just want to expose the names of the properties and their values, use Reflection. Take a look at this. Add some behind code to create a collection of pairs with names and values. That should do the trick
Then use some DataTemplate to create the textboxes from this info. I find this tutorial very useful
I used this to solve my problem
I apologize for the crappy title, I wasn't quite sure how to summarize what I'm trying to do.
My scenario:
I am refactoring an existing WinForms application to WPF and MVVM. I'm currently working on a module that provides search functionality for a database comprised of many different tables such as Contact, User, Case, Product, etc. In code-behind there are classes which provide an Object for each. I have written wrapper classes for each of the searchable table Objects that expose only the properties a user would want/need to see in the search results for each type of Object, for binding to a DataGrid.
Once these search results exist, they need to be displayed in a combination of Tab Controls and Data Grids, like so:
Because of some use cases, I need to be able to create an individual display Tab+DataGrid for the results of every single search that is performed. I cannot have a single tab for each type of search that is shown/hidden as needed.
I'm not sure how to proceed from where I currently am to the goal pictured and described above. Thanks for any help anyone can provide.
Not sure I entirely understand your question, but it looks to me that it might be a candidate for datatemplateselector.
Basically, you use an ItemsControl bound to your result collection and then - using a datatemplateselector - you swap in the appropriate template to display the item based upon a code inspection.
That will let you present all the results in a single list.
If you want to display your results in a tabs, I would present a collection of each result type in your viewmodel. So you have a Users collection and a seperate Products collection. Then you can bind individual data grids to each template.
If you want to then hide the tabs when no results are present, add a data trigger using the expression.interactivity namespace to trigger the visibility of each tab page based on its respective collection count.
One more thing, if you want to create tab items dynamically, i.e. One tab for each search - the tab control has an ItemSource property. If you group each search result into an object an expose an observable collection of that object, you can bind that to your tab control and have it create tab items for each search result. Just make that object contain a collection of actual results and you should be able to create a itemscontrol as mentioned here already.
Sorry if this appears a bit of a mind dump, but like I said - not sure if I entirely get the question :)
Rather then chuck a load of code snippets in, there a plenty of samples just a google away if anything sounds helpful.
Hej,
I'm trying to make som general functionality for my ListView, so that the content of a ListView can be exported to CSV directly.
I'm trying to achive this by getting the datacontext and analysing the ICollectionView for this. From here I have access to the all the objects from via ICollectionView via SourceCollection, in which I (for now) presume sorting/and filtering is respected.
The challange here is that I only want to output the columns that also are showed in the ListView.
When iterating my collection, is there a function where I can evluate if a property in a class (with notification suppoert) has a binding to it?
The esiest solution for now would be just to output all properties, but I'm not interested in this, since oid's are not fun to look at.
Thx in advanced.
/Ian
I suggest you recognize that the ability to determine which data is being displayed is a business requirement. Thus, you should embody this requirement in your model. In other words, your model should clearly indicate which columns are visible - you shouldn't be trying to infer this from your existing properties, nor should you be examining your view.
There are a whole bunch of ways you could do this, but the key is to have this information on hand in your model.
Why don't you just look at the DataTemplate and evaluate it including the binding inside?
There's no easy way to do it... You can check the binding on the target side (dependency property), but not on the source side.
For what you're trying to do, you could loop through the columns of the ListView and check their DisplayMemberBinding, but it could be undefined (the cell content might be defined using the CellTemplate property instead).