通过ItemsControl向ComboBoxItem添加样式

时间:2018-03-27 09:53:12

标签: c# wpf

以下是带错误的代码。

当我直接使用ComboBox而不是ItemsControl时,没关系。

但问题是如何通过ItemsControl来做到这一点?我必须使用这个作为WPF,在ComboBox的情况下,当我没有添加任何内容时添加空的ComboBox(添加的元素是动态的)。我无法摆脱他。 ItemsControl按预期添加,即仅在我动态添加时才添加。

<ComboBox ItemsSource="{Binding ConnectorItemsY}">
    <ItemsControl.LayoutTransform>
        <RotateTransform Angle="270"/>
    </ItemsControl.LayoutTransform>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <RotateTransform Angle="90" />
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ComboBox>

ConnectorItemsY - &gt; ComboBox的集合

编辑:

下面的代码按我喜欢的方式旋转,但使用的是ComboBox。

/**
* @ORM\OneToMany(targetEntity="Planification", mappedBy="zone")
*/
protected $planifications;

enter image description here enter image description here

左图是旋转的ComboBox。单击右视图并旋转内容后。我希望它只在ItemsControl的帮助下不使用ComboBox xaml文件。

3 个答案:

答案 0 :(得分:1)

您必须将样式添加到ItemsControl.Resources,才能将其应用于内部的所有ComboBoxItems

<ItemsControl ItemsSource="{Binding ConnectorItemsY}">
    <ItemsControl.Resources>
        <Style TargetType="ComboBoxItem">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <RotateTransform Angle="0" />
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.Resources>
    <ItemsControl.LayoutTransform>
        <RotateTransform Angle="270"/>
    </ItemsControl.LayoutTransform>
    <ItemsControl.Items>

    </ItemsControl.Items>
</ItemsControl>

Style实际上不是控件,所以你不能将它放在像这样的控件旁边的XAML树中。这只是一个&#34;蓝图&#34;在样式下方的XAML树中设置给定类型的任何控件的某些属性,并且必须将其放入控件的Resources集合中。在这种情况下,您定位ComboBoxItem,这意味着ComboBoxItemsControl.Items控件内的所有项都应该应用您的样式。

您还可以使用Stylex:Key命名,然后使用Style="{StaticResource KeyOfTheStyle}"

手动将其应用到您认为合适的任何位置

答案 1 :(得分:0)

如果您希望将ComboBoxItem样式应用于ComboBoxItem源代码集合中的所有ConnectorItemsY元素,则只需将<ItemsControl.Items>更改为<ItemsControl.Resources>即可:

<ItemsControl ItemsSource="{Binding ConnectorItemsY}">
    <ItemsControl.LayoutTransform>
        <RotateTransform Angle="270"/>
    </ItemsControl.LayoutTransform>
    <ItemsControl.Resources>
        <Style TargetType="ComboBoxItem">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <RotateTransform Angle="0" />
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.Resources>
</ItemsControl>

如果您想要“退回”项目,还应指定-270的角度:

<Style TargetType="ComboBoxItem">
    <Setter Property="FontSize" Value="20" />
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <RotateTransform Angle="-270" />
        </Setter.Value>
    </Setter>
</Style>

答案 2 :(得分:0)

也许&#34; containeritems&#34;指所有物品的容器。

在这种情况下,您可以旋转itemspresenter而不是单个项目。 快速实验:

   <ItemsControl ItemsSource="{Binding Items}">
        <ItemsControl.LayoutTransform>
            <RotateTransform Angle="270"/>
        </ItemsControl.LayoutTransform>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{Binding SubItems}"
                          >
                    <ComboBox.Resources>
                        <Style TargetType="ItemsPresenter">
                            <Setter Property="LayoutTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="-270" />
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ComboBox.Resources>
                </ComboBox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

和我的viewmodel:

public ObservableCollection<object> Items { get; set; } = new ObservableCollection<object>
    {
      new {Name="Item 1", SubItems= new List<int>{11,22,33,44 } },
      new {Name="Item 2", SubItems= new List<int>{11,22,33,44 } },
      new {Name="Item 3", SubItems= new List<int>{11,22,33,44 } },
    };

当我下拉组合(侧身)时,列表会垂直显示。