how to make 2 controls share the same listbox - wpf

Is it possible to have a listbox displaying its items with a control template to change their look in the listbox and also have a canvas displaying the listboxItems with another look?
To be more accurate,
I have a UserControl named InfoControl and another named DesignerControl with the same DataContext: a DesignerVM which contains an ObservableCollection of CurveVM.
In my InfoControl, I have a listbox with its items binded to the OC and shown as stackpanels using a template.
Now I want to display my items in the DesignerControl (canvas) but with another look.
Should i have to create another listbox and synchronize it with the first one or is there a way to achieve my goal?

Two controls shouldn't share a listbox. Instead, make two separate listboxes and bind them to the same ItemsSource or DataContext. Use a DataTemplateSelector to control which template is used.
Edit: And welcome to StackOverflow :)

Related

How can I apply AutoGenerateColumn to apply DataGridTemplateColumn.HeaderTemplate in a DataGrid?

for an overview, an MVVM project in built in WPF.
Basically in my xaml, I have a datagrid bound to a dataview. When my service populates the dataview I get a dynamic table with an arbitrary number of columns.
For each column in the datagrid, I have created a headertemplate which contains a combobox which is bound to an Observable<Dictionary<string,BusinessEntity>> object as its item source in the xaml. Figuring out the combobox is another issue but I am trying to just populate data grid first, then worry about binding the combobox correctly.
anyways the only solution I have somewhat though of was to turn on autogeneratecolumn and then replacing all the headers with a combobox in the codebehind, but then I have issues trying to bind the combobox in the codebehind correctly and it doesn't feel MVVM if I have to create all those comboboxes there.
You can handle the AutoGeneratingColumn event to customize the auto generated column's header template.

WPF - How to extend an existing toolbar UserControl

I create a new UserControl for reuse. The UserControl contains a toolbar with several Buttons.
When I use the UserControl, I hope I can add additional Buttons on it but can not modified the orginal ones.
This is easily implemented in WinForm by inheritance. But in WPF, I have no idea how to do it.
There is a few ways to do what you need.
For example, your UserControl could expose ICollection TabButtons property, which is ItemsSource for toolbar. In this case, your toolbar should contain some ItemsControl to host buttons and should be binded to aforementioned TabButtons property.
And TabButton struct should describe all needed information (for example, properties Header, Icon, ToolTip and Command).
Or you could provide appropriate methods: AddToolbarButton(), RemoveToolbarButton(). This methods change TabButtons collection (but the collection itself cann't be accessed from outer realm).

How to Bind Grid with Properties in Code behind?

I have a grid in a XAML file and I want to generate its RowDefinition dynamically. To do that I create a List<RowDefinition> in which I add 3-4 RowDefinitions. Now I have to bind this property to the grid in the XAML file. How do I do this?
If you want to host dynamic content (varying number of items), the Grid control might not be your best option. Try using an ItemsControl (which can be templated to look however you want) and bind your actual data against the ItemsSource property. If you post some more information about what kind of content you are looking to display I can give you an example of how to do it with an ItemsControl.

WPF - Adding a Rowheader to a ListView Grid View

I need to add a rowheader to a Listview GridView. it needs to act like a datagrid rowheader, sop it should be freezable.
is thsi possible? can anyone show me some source code to do this?
Thanks
The GridView do not have rowheaders but you can style the first column differently than the rest. Implementing freezing will require some innovative use of a scrollviewer. Can't you just use the DataGrid which has RowHeaders?
DataGrid Class

Change foreground/background color of Item in Listbox programmatically in WPF

How can i add different items with either background/foreground color different in WPF listbox?
You can can create a DataTemplete, set it to ListBox.ItemTemplate and use DataTrigger to change the display of the items.
ListBox has some very annoying behaviors that make styling it using data template difficult - in this blog post you will find the list of workarounds.
You may use an AlternationCount property, more info provided by this link
Or you can add to your objs binded to a listbox Background and Foreground properties and bind them in a DataTemplate of ListBox.ItemTemplate and change them in code however you like, properties must update themselves on every changing.
After tearing my hair out with this one (and your particular usecase might differ from mine) I found the WPF Toolkit's DataGrid cured all my needs.

Resources