How to style ItemsPresenter in WPF? - c#

I have a project which contains a timeline view (this one). I need the background of this timeline view to be transparent (as I am rendering other objects/controls behind it). There is an element which is coloured Opaque, so I need to change this to transparent.
If I inspect the elements in the view during debug execution, I can see that the element which needs to be changed is an ItemsPresenter (see image below). The ItemsPresenter contains a StackPanel, and when changing the colour of this stackpanel by editing its properties in the live visual tree, the issue is resolved.
...How do I add a style for the ItemsPresenter control that can be used either globally or specifically by the timeline, changing the background of the stackpanel it contains? Are you able to provide an example?
Many thanks for your help.

The ItemsControl has an ItemsPanelTemplate that you can set the Background property of:
<ItemsControl ...>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Background="Gray"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
...
</ItemsControl>

Related

auto-fit Grid Columns in UWP XAML

I'm using GridView to create a grid of items, and im trying to make the columns fill to expand the remaining space. Similar to how auto-fit behaves in CSS. See example..
Here I've set the MaxWidth of the GridView's children to 350px, and then when the window is not an even multiple of 350px + padding, I would like each column to expand to fill the remaining space.
Is this possible with UWP/XAML?
If you want to achieve the auto-fit effect, you can try to use UniformGrid Control from Windows Community Toolkit. It is a responsive layout control which arranges items in a evenly-spaced set of rows or columns to fill the total available display space. First, you need to add Microsoft.Toolkit.Uwp.UI.Controls nuget package and then use it as GridView's ItemsPanel. For example:
.xaml:
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<controls:UniformGrid Columns="3" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>

How can I design a region settings like this in WPF

Till now I have a combo-box control which displays all the available region language in the UI as a combo-box items.
its in WPF and MVVM
<ComboBox
x:Name="cbLanguage"
Grid.Row="1"
Height="30"
Width="200"
HorizontalAlignment="Center"
VerticalAlignment="Top"
ItemsSource="{Binding LocalLanguages,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="0">
But I saw this window and thought this looks much more legant and modern.
Can I do similar kind of window in WPF.
I tried to change the Combobox with list-box, List-view but no result.
Any help if there is any control in WPF which can do this.
This solves many problem specially if the combo box have more than 10 items user has to scroll through the all and then select the last index. But in this way user can select any locals as all are displayed in the UI. Even user can have the option to display alphabetically.
arraging items in 3 columns can be achieved by using UniformGrid as ItemsPanel
<ComboBox.ItemsPanel>
<ItemsPanelTemplate >
<UniformGrid Columns="3"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
modify ItemTemplate to change items apperance and have green color for selected item
i think it is necessary to modify ComboBox template to have custom header and footer in a dropDown (Edit Template-Edit a Copy in Visual Studio designer)
In WPF combobox is basically a Popup when the toggle button is pressed.
You could implement your own popup or you could have a look at the template of the ComboBox. Here is a link to ComboBox template

Get an Image that is the child of a Panel

I have a panel and I added it in a Window as ItemsPanel of an ItemsControl
<Grid x:Name="outerGrid" >
<ItemsControl ItemsSource="{Binding ImageSourcesCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<view:CustomPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
As you can see in this panel I show a series of images. Everything works all right, but now I want to access those images from inside the CustomPanel to change the Source to one of them.
If I access them as elements of base.Children I obtain a ContentPresenter, i.e.:
var element = base.Children[i]; //<- this is a ContentPresenter
So my question is: how can I get the Image?
One solution, since I know the position of the image, would be to get the element at that position. But I would prefer something else because it is not really clean and if I have other images moving around it can be a source of troubles.
Thanks!
I found out that if you just want to change the source of the Image (and this was my case), you can change the property Content of the ContentPresenter. Not sure how to access the Image though...

Bind StackPanel to method that adds child elements

Here's my issue: I have a wrapper class that contains sets of lists that contain 15 images each. I want to bind a central StackPanel to a method that actually modifies the same StackPanel that was passed to it and adds child StackPanel elements that contain 15 images each.
To clarify:
I have a central StackPanel that has a vertical orientation. This StackPanel is located inside of a DataTemplate!.
<DataTemplate>
<Grid x:Name="ImageDisplayGrid" Height="861" Width="656">
<StackPanel x:Name="CentralImagePanel" HorizontalAlignment="Left" Height="841" Margin="10,10,0,0" VerticalAlignment="Top" Width="636"/>
</Grid>
</DataTemplate>
I have many instances of my wrapper class that contain up to 15 images each (as WritableBitmap objects.
I want to bind my central StackPanel to some method that will modify that StackPanel, iterating through my list of wrapper classes and adding child StackPanel controls to the central StackPanel for each instance of my wrapper class found.
For each instance of the (ImageSet1, ImageSet2, etc for example) wrapper class, the new StackPanel that will be added to the central StackPanel will be populated using the images contained in that wrapper class instance.
In my mind there isn't really anything to be 'returned' here, so I was hoping there was a way to just pass the control (the central StackPanel) to some method, let the method modify it, and then carry on after the central StackPanel is populated with its child `StackPanels'.
To clarify even more:
Think of NetFlix. You know how you can scroll vertically through each category and each category allows you to scroll horizontally? Thats what I am trying to emulate, only I want it to be dynamic and bound to my wrapper class that contains a list of Images to use.
My main obstacle right now is that the central StackPanel is located within a DataTemplate, so there isn't an easy way to access it during runtime. On top of that it would be nicer to use a binding anyway.
I have tried to use IValueConverter to turn my wrapper class into a list of StackPanel objects that the central StackPanel can use, but that didn't work. I've also searched for ways to bind a control to a method that has no return property without any luck as well.
Any help or examples would be greatly appreciated.
You are thinking about this wrong. Really, really, wrong. StackPanel is a layout control. You shouldn't ever be directly modifying its children or any other properties.
As you've noticed, there is no real way to do this task in the way you describe.
To display collections, use an ItemsControl. In your case, it would be something like:
<ItemsControl ItemsSource="{Binding Categories}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource={"Binding Videos"}>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- Whatever -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Notice the inner template is another ItemsControl, this time with a horizontal StackPanel as the panel template.

Best Control to add Multiple UserControl in WPF MVVM?

I have a MainWindow in which i want to add my others Views.
Users can open Mutliple number of different Views to the MainWindow.
So for adding all those Views(UserControls), which is the best control to use.
Current I am using Canvas, but its not supporting MVVM.
So how can i add multiple control.
This is what i have done till now
Note:
The control should host multiple UserControl by the same time i should be able to drag One UserControl here and there in that control and then on a click on UserControl should bring it to Front(Focused), which i did in Canvas using ZIndex.
Proper MVVM solution migh be to use ItemsControl class and bind collection of view-models as an ItemsSource.
In DataTemplate of that ItemsControl, I would specify proper view for child view-models (sort of tool-window in your case).
<ItemsControl.ItemTemplate>
<DataTemplate>
<views:ToolWindow DataContext={Binding} />
</DataTemplate>
</ItemsControl.ItemTemplate>
To achieve window-like behavior as on your picture I would specify custom panel based on Canvas which would allow drag and drop behavior.
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<panels:MyCustomMdiPanel />
</ItemsPanelTemplate>
<ItemsControl.ItemsPanel>
I suppose that you have already working canvas solution.

Categories

Resources