So, i have quite complex ListBox, with own style and composite item templates (in every item, there are image and text, both with click events, which rises corresponding commands in global viewmodel). What would be the easiest way to re-use that ListBox?
Should i extract style only, or move it out as UserControl, or should i make new component? Everything is static (commands, markup) exept content (should be able to use different items lists).
In the perfect case, is should be (pseudocode):
Page 1 xaml
<MyListBox Content={Binding Page1List}>
Page 1 vm
ObservableCollection<Items> Page1List;
and so on for other pages.
EDIT: i found this article - that makes sence, but i think there should be easier way for that.
http://www.codeproject.com/Articles/14340/Creating-a-look-less-custom-control-in-WPF
I believe the solution you are looking for is a look-less control.
Related
I'm creating a WPF program that consumes rest api data. I want to implement lazyloading and infinite scroll on the data and programmatically create and add either custom data templates or listitems very similar to this design
I'm just confused as to which approach to take and what benefits/costs each provides
Easy choices:
Everyone uses MVVM so use MVVM.
Data Templating is a fundamental of wpf and building UI in code is not recommended - so use data templating.
You can dynamically add templates to resources by building xaml as strings. This is the MS recommended way to build any dynamic UI. Those strings can come from flat files, a database directly or a web service and you can build them by manipulating txt files or serialising controls.
A huge plus of this is you have the markup "right there". So when things go pear shaped you can paste into an experimental solution and see the errors light up in the xaml or see what the user is seeing.
If datatype associated templating doesn't suit for some reason then you could write a datatemplateselector and put your logic in there.
I'm not sure how you expect that to scroll exactly but I'd go with a listbox, some datatemplates associated with a type per view. Assuming the items can have different views - you just seem to have that "gilded" button or tag as an option.
Load your data into viewmodels with one per row.
.Add to an Observablecollection which is a public property in a viewmodel.
Bind that to the itemssource of a listbox.
They are then templated into UI.
A listbox has a scroller built in but you could re-template if you wanted to scroll using some other approach.
A StackPanel is a Panel that arranges child elements into a single line that can be oriented either horizontally or vertically.
A ListView is an ItemsControl that you can bind to an IEnumerable of objects and is used to present a collection of items.
What you should do is to create an ItemsControl with an ItemTemplate that corresponds to a scrollable item in the list. There is a basic example available here and you will find a lot more examples online.
I'm pretty new to WPF/XAML in C#, I saw some simple tutorials and stuff but, today I'm confused, never used to make UI there.
I need to output multiple forms which instance would look something like that:
(NB: I constructed it just in WYSIWG way, so XAML markup is total mess, it for screenshot. I cant even add table directly in grid, oh)
I got data like this for each month in schoolyear, so, depends on what class is choosen I want it to output like that (scheme)
(NB: instead of each CustomWindowInstance there I need my form from 1 st screenshot)
So, whats is best and simplies approach for that?
EDIT 1: I'll name XAML from the first screenshot, which represents controls you want to display multiple times, XAML 1, and another one, where you want to do it, XAML 2.
--
Almost ORIGINAL:
Create a separate User Control and move the content of Window tag from XAML 1 into it. Then you'll be able to show it as many times as you want by placing <yourUserControlNamespace:YourUserControlClass /> in XAML 2.
For an ability of communication with this control in XAML like <yourUserControlNamespace:YourUserControlClass Parameter="SomethingHere" /> you should implement a dependency property with name ParameterProperty (desired name of property in XAML + "Property") in YourUserControlClass.xaml.cs.
--
EDIT 2: One may also create a DataTemplate with parameter DataType={x:Type yourVMNamespace:YourVMClass} for the XAML 1's ViewModel (not View) in XAML 2's or the whole application's Resources. In that case the ViewModel class you are binding to will be represented by the DataTemplate's content itself. Later you can write <yourVMNamespace:YourVMClass /> as in the first solution.
This approach allows displaying rich representation of any items themselves, for example, in controls derived from ItemsControl, such as ListBox, all you've got to do is to bind ListBox's ItemsSource property to a collection of YourVMClass instances (or specify them in XAML by hand).
More precisely, in this case I won't call YourVMClass a ViewModel.
I need to make a deep copy of a tabcontrol.
The large picture is this: I have a project which has a 300 line XAML code TabControl with 8 tabs in it which are pretty big. I also have a TreeView with different items.
When an item in that list is selected it shows the TabControl associated with it. The problem now comes that when I add an element I want to make a deep copy of the original TabControl and associate that new one to the new element (of course I'm going to erase the content in the new one). Shallow copies won't work because they are pointing to the same location in memory, so "=",IClonable are a no go. And the frustrating part is that I can't use deep copy with serialization because the TabControl is not serializable. And I can't (or should say won't) make a custom TabControl which is serializable because the TabControl is 300 line in XAML and it would be 600 line in code so it's a waste of space and time.
I've searched for this for 2 days and didn't find anything. There is no need for me to show the code because I'm looking for a general purpose Deep Copy method that can copy any type of a TabControl.
You're doing it all wrong.
You're not supposed to "copy" the UI or whatever, because UI is Not Data. You should be manipulating and copying Data Items instead of UI elements, and probably using DataTemplates to have your Data items represented on screen.
I strongly suggest that you research and learn MVVM before ever writing a single line of code in WPF.
After searching some more I initially tried a different way but it turned out to be more trouble than it's worth (tried with data binding and working more in code but still to much).
So the solution is to use XamlReader and XamlWriter. Official documentation is found here and respectively here !
To answer my question in code it would be:
Say you had this tabcontrol in XAML here:
<TabControl>
<TabItem><!--A lot of stuff here--></TabItem>
<TabItem><!--More stuff here--></TabItem>
</TabControl>
And remember this is if you have a lot of stuff (basically I didn't mention this but I'm making an interface that creates a pretty complex XML so in that TabControl I'm handling a lot of user generated data)! If you have a simple TabControl just make a custom one in code or simply use DataBindings.
So the code in the background for making a Deep Copy of that XAML defined TabControl would be this:
string savedTabControl = XamlWriter.Save(originalTabControl);
StringReader stringReader = new StringReader(savedTabControl);
XmlReader xmlReader = XmlReader.Create(stringReader);
TabControl newTabControl = (TabControl)XamlReader.Load(xmlReader);
So this is basically serialization made on xaml controls and not on data.
I'm pretty new to c#, the first thing that I'm trying to make is a ListView with data bindings which has turned out ok.
I'm now trying to make items have a twist button if the underlying model has any children (like the TreeView). Each of the children will have columns the same as all the top level items.
How would I go about doing this? Is there an already existing control like this? If not would I be better off dressing up a TreeView to look like a ListView, or dress up a ListView to look like a TreeView?
I went down the road outlined in this solution which dresses up a TreeView, but the end result looks pretty awful and the heading is actually just an item, so you lose all the nice column sizing and column buttons that can hook up to column sorting that you get in ListView so that route actually seems like it would be more work.
I noticed the new task manager has a control exactly like what I'm trying to create, I don't know how this made? probably in C though.
Microsoft provides a sample that appears to be what you are looking for. A write-up of the example can be found here:
http://msdn.microsoft.com/en-us/library/vstudio/ms771523(v=vs.90).aspx
When you build and run the example you will end up with something resembling this:
There is a large amount of templating done in the example, so you will be able to make things look the way you want.
What you describe sounds a bit like a TreeListView, and if you google 'WPF TreeListView' you will see some solutions that might be good for you. I have used one from Telerik, but it might be overkill depending on how complicated your needs are.
If you only want one sub-level like the image you attached, you might want to just roll your own using a ListView with a complex DataTemplate for the first column which would show an expander button and a simple ListBox bound to the children items.
Similar to the answer here, except your cell would have a checkbox styled to look like the arrow, the text for the item, and a child ListBox. Then bind the visibility of the child ListBox to the state of the checkbox.
I was wondering if there was a way to share a listbox on the same page.
Basically, I have a listbox that is bound to a collectionviewsource. I also have 5-6 pivot controls that will use the same listbox, but will filter the collectionviewsource differently.
I was wondering if it was possible to say have a listbox as a resource and display it on all hte pivots then when the pivot changes, I can apply a different filter to the collectionviewsource.
Can anyone steer me the right way?
Thanks!
You could create an ItemTemplate for the Pivot which includes the listbox.
I find it easier to maintain this scenario by explicilty creating each pivotItem in XAML but having all teh listboxes share the same ItemTemplate though. I find it easier to see what's going on by just looking and it also makes it easier if you ever need to change the template of one of the listboxes. (I find this seems to happen quite often.)