将样式应用于某个(命名的)堆栈面板(或控件)的所有子元素
Applying a style to all child elements of a certain (named) stackpanel(or control)
我试图简单地定义第二个堆栈面板(根堆栈面板中的两个)的所有矩形元素的填充,所需的填充是白色的。由于堆栈面板没有 ItemTemplate,我尝试用 ItemsControl 替换它,然后封装它。看到ItemsControl没有Orientation属性就出现了stack panel的封装。
无论如何,子矩形仍然没有被白色填充...>.<我做错了什么?
(我也在尝试对事件绑定做同样的事情,就像 How to set an event function via a style? 我还在尝试一些事情,我想一开始我也想通过 ItemControl 来做,但 atm 都不起作用:/ 不是我的一天)
<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>
后面的代码,如果你想试试看:(在第二行,第三个方块可以通过拖放接收颜色,而第二个有所有事件但没有我正在尝试的所需填充)(DutchVarsToEnglish:rechthoek 表示正方形,kleur 表示颜色,gesleepteKleur 表示 draggedColor,sleep 表示拖动):
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 所以 pastebin link 到代码隐藏)
在 StackPanel 的资源中声明一个默认的矩形样式:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="White"/>
</Style>
</StackPanel.Resources>
<Rectangle .../>
...
</StackPanel>
如果您在元素树中有另一个默认的矩形样式,您可以将您的 "local default Style" 基于该样式,即样式的 BasedOn
属性,例如
<StackPanel.Resources>
<Style TargetType="Rectangle" BasedOn="{StaticResource {x:Type Rectangle}}">
<Setter Property="Fill" Value="White"/>
</Style>
</StackPanel.Resources>
你可以定义一个 class 有一个 Fill
属性:
public class Item
{
public Brush Fill { get; set; }
}
并将 ItemsControl
的 ItemsSource
属性 设置为此类对象的集合:
public partial class WindowRegenboog : Window
{
public WindowRegenboog()
{
InitializeComponent();
DropZone.ItemsSource = new List<Item> { new Item { Fill = Brushes.Red }, new Item() { Fill = Brushes.Yellow } };
}
}
然后将 ItemTemplate
中 Rectangle
的 Fill
属性 绑定到 Fill
来源 属性:
<ItemsControl Name="DropZone" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Rectangle Fill="{Binding Fill}" Width="200" Height="200"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这样您就可以更改 Rectangle
的 Fill
颜色,只需将源集合中某个项目的 Fill
属性 设置为不同的画笔即可。
我试图简单地定义第二个堆栈面板(根堆栈面板中的两个)的所有矩形元素的填充,所需的填充是白色的。由于堆栈面板没有 ItemTemplate,我尝试用 ItemsControl 替换它,然后封装它。看到ItemsControl没有Orientation属性就出现了stack panel的封装。
无论如何,子矩形仍然没有被白色填充...>.<我做错了什么?
(我也在尝试对事件绑定做同样的事情,就像 How to set an event function via a style? 我还在尝试一些事情,我想一开始我也想通过 ItemControl 来做,但 atm 都不起作用:/ 不是我的一天)
<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>
后面的代码,如果你想试试看:(在第二行,第三个方块可以通过拖放接收颜色,而第二个有所有事件但没有我正在尝试的所需填充)(DutchVarsToEnglish:rechthoek 表示正方形,kleur 表示颜色,gesleepteKleur 表示 draggedColor,sleep 表示拖动):
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 所以 pastebin link 到代码隐藏)
在 StackPanel 的资源中声明一个默认的矩形样式:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="White"/>
</Style>
</StackPanel.Resources>
<Rectangle .../>
...
</StackPanel>
如果您在元素树中有另一个默认的矩形样式,您可以将您的 "local default Style" 基于该样式,即样式的 BasedOn
属性,例如
<StackPanel.Resources>
<Style TargetType="Rectangle" BasedOn="{StaticResource {x:Type Rectangle}}">
<Setter Property="Fill" Value="White"/>
</Style>
</StackPanel.Resources>
你可以定义一个 class 有一个 Fill
属性:
public class Item
{
public Brush Fill { get; set; }
}
并将 ItemsControl
的 ItemsSource
属性 设置为此类对象的集合:
public partial class WindowRegenboog : Window
{
public WindowRegenboog()
{
InitializeComponent();
DropZone.ItemsSource = new List<Item> { new Item { Fill = Brushes.Red }, new Item() { Fill = Brushes.Yellow } };
}
}
然后将 ItemTemplate
中 Rectangle
的 Fill
属性 绑定到 Fill
来源 属性:
<ItemsControl Name="DropZone" HorizontalAlignment="Center">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Rectangle Fill="{Binding Fill}" Width="200" Height="200"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这样您就可以更改 Rectangle
的 Fill
颜色,只需将源集合中某个项目的 Fill
属性 设置为不同的画笔即可。