将样式应用于某个(命名)stackpanel(或控件)的所有子元素

时间:2017-02-20 12:00:33

标签: c# wpf wpf-controls wpf-style

我试图简单地定义第二个堆叠面板(根堆栈面板中的两个)的所有矩形元素的填充,所需填充为白色。由于stackpanel没有ItemTemplate,我尝试用它替换它,然后用ItemsControl封装它。当我看到ItemsControl没有Orientation属性时,堆栈面板的封装就出现了。

无论如何,在所有这些之后,孩子的矩形仍然没有充满白色......>。我究竟做错了什么?

(我也试图像How to set an event function via a style?那样对事件绑定做同样的事情我仍然在那里尝试,我想我最初也想通过ItemControl这样做,而不是在运行时:/不是我的一天)

<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>

代码背后如果你想尝试一下:(在第二行,第三个方块可以通过拖放接收颜色,而第二行包含所有事件但不是我需要的填充#39; m试图拥有它)(DutchVarsToEnglish:rechthoek表示square,kleur表示颜色,gesleepteKleur表示draggedColor,sleep表示拖动)

https://defuse.ca/b/c19ncFwllWIpSRe6I0cclp(我无法弄清楚如何折叠C#片段,就像这里的js片段一样https://meta.stackexchange.com/questions/70885/collapse-code-snippets-in-answers:S so pastebin link to codebehind)

2 个答案:

答案 0 :(得分:1)

在StackPanel的参考资料中声明一个默认的Rectangle Style:

runOutsideAngular

如果您在元素树中有更高的默认矩形样式,您可以将该样式的“本地默认样式”作为样式的<StackPanel> <StackPanel.Resources> <Style TargetType="Rectangle"> <Setter Property="Fill" Value="White"/> </Style> </StackPanel.Resources> <Rectangle .../> ... </StackPanel> 属性的基础,例如

BasedOn

答案 1 :(得分:1)

您可以定义一个具有Fill属性的类:

public class Item
{
    public Brush Fill { get; set; }
}

并将ItemsSource的{​​{1}}属性设置为此类对象的集合:

ItemsControl

然后,您将public partial class WindowRegenboog : Window { public WindowRegenboog() { InitializeComponent(); DropZone.ItemsSource = new List<Item> { new Item { Fill = Brushes.Red }, new Item() { Fill = Brushes.Yellow } }; } } Fill的{​​{1}}属性绑定到Rectangle源属性:

ItemTemplate

这样,您只需将源集合中项目的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> 的{​​{1}}颜色。