I'm trying to simply define the fill off all rectangle elements of the second stackpanel(of the two within the root stackpanel), the needed fill being white. Since a stackpanel doesn't have ItemTemplate I've tried replacing it with, and then encapsulating it with, ItemsControl. The encapsulation of the stack panel came to be when I saw that ItemsControl doesn't have an Orientation property.
Anyhow after all this the child rectangles still don't get filled with white... >.< what am I doing wrong?
(I also am trying to do same with the event bindings just like How to set an event function via a style? I'm still trying things out there, thought I wanted to do it via ItemControl as well initially, not working either tho atm :/ not my day)
<Window x:Class="RegenboogDragDrop.WindowRegenboog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowRegenboog" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Width" Value="50"></Setter>
<Setter Property="Margin" Value="5"></Setter>
<Setter Property="Stroke" Value="Black"></Setter>
<Setter Property="StrokeThickness" Value="3"></Setter>
</Style>
</Window.Resources>
<StackPanel>
<StackPanel Margin="0,50" Orientation="Horizontal" HorizontalAlignment="Center">
<Rectangle Fill="Yellow" MouseMove="Rectangle_MouseMove"></Rectangle>
<Rectangle Fill="Orange" MouseMove="Rectangle_MouseMove"></Rectangle>
<Rectangle Fill="Red" MouseMove="Rectangle_MouseMove"></Rectangle>
<Rectangle Fill="Blue" MouseMove="Rectangle_MouseMove"></Rectangle>
<Rectangle Fill="Green" MouseMove="Rectangle_MouseMove"></Rectangle>
<Rectangle Fill="Violet" MouseMove="Rectangle_MouseMove"></Rectangle>
<Rectangle Fill="Indigo" MouseMove="Rectangle_MouseMove"></Rectangle>
</StackPanel>
<ItemsControl Name="DropZone" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Rectangle Fill="White"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Name="dropRed" Fill="White"/>
<Rectangle Name="dropOrange" MouseMove="Rectangle_MouseMove" DragDrop.DragEnter="Rectangle_DragEnter" DragDrop.DragLeave="Rectangle_DragLeave" DragDrop.Drop="Rectangle_Drop" AllowDrop="True"></Rectangle>
<Rectangle Name="dropYellow" Fill="White" MouseMove="Rectangle_MouseMove" DragDrop.DragEnter="Rectangle_DragEnter" DragDrop.DragLeave="Rectangle_DragLeave" DragDrop.Drop="Rectangle_Drop" AllowDrop="True"></Rectangle>
<Rectangle Name="dropGreen"></Rectangle>
<Rectangle Name="dropBlue"></Rectangle>
<Rectangle Name="dropIndigo"></Rectangle>
<Rectangle Name="dropViolet"></Rectangle>
</StackPanel>
</ItemsControl>
<Button Name="ButtonCheck" Content="Check volgorde" Margin="5,50"></Button>
</StackPanel>
Code behind if you want to try it out: (On the second row the third square can receive colors by drag and drop, while the second has all the events but not the needed fill that I'm trying for it to have) (DutchVarsToEnglish: rechthoek means square, kleur means color, gesleepteKleur means draggedColor, sleep means drag):
https://defuse.ca/b/c19ncFwllWIpSRe6I0cclp (I can't figure out how to collapse a C# snippet just like the js snippet here https://meta.stackexchange.com/questions/70885/collapse-code-snippets-in-answers :S so pastebin link to codebehind)
Declare a default Rectangle Style in the Resources of the StackPanel:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="White"/>
</Style>
</StackPanel.Resources>
<Rectangle .../>
...
</StackPanel>
In case you have another default Rectangle Style higher in the element tree, you can base your "local default Style" on that Style be means of the Style's BasedOn
property, like
<StackPanel.Resources>
<Style TargetType="Rectangle" BasedOn="{StaticResource {x:Type Rectangle}}">
<Setter Property="Fill" Value="White"/>
</Style>
</StackPanel.Resources>