2011-04-17 60 views
1

我想要在WPF中使用组合框来存储一些名称,但我希望组合框本身是垂直的,并且当您单击它时,它应显示每个项目与另一个45°旋转,使其更具可读性。喜欢的东西:L////WPF组合框 - 垂直显示与45°旋转项目

我取得了一些它这样做:

     <ComboBox Name="combo" 
            Grid.Row="0" Grid.Column="1" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center"> 
          <ComboBox.LayoutTransform> 
           <RotateTransform Angle="270" /> 
          </ComboBox.LayoutTransform> 
          <ComboBox.Items>         
           <TextBox> 
            <TextBox.LayoutTransform> 
             <RotateTransform Angle="315" /> 
            </TextBox.LayoutTransform> 
           </TextBox> 
          </ComboBox.Items> 
         </ComboBox> 

我填的是组合框是这样的:

 m_Main.combo.Items.Clear(); 
     foreach (PlayerInfo player in m_CurrentData.PlayersInfo) 
     { 
      m_Main.comboPlayer1.Items.Add(player.Name);      
     } 

但只有第一项被旋转,加我在实际项目上获得一个空白项目(我在运行时填充组合框项目)。 我在做什么错?

编辑:没有更多的空白项目,因为我清除项目。

回答

3

这工作(如果我理解正确的,你想要什么)

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Grid> 
    <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="100"> 
     <ComboBox.LayoutTransform> 
     <RotateTransform Angle="270" /> 
     </ComboBox.LayoutTransform> 
     <ComboBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical" IsItemsHost="True"> 
      <StackPanel.LayoutTransform> 
       <RotateTransform Angle="90" /> 
      </StackPanel.LayoutTransform> 
      </StackPanel> 
     </ItemsPanelTemplate> 
     </ComboBox.ItemsPanel> 
     <ComboBox.ItemContainerStyle> 
     <Style TargetType="ComboBoxItem"> 
      <Setter Property="LayoutTransform"> 
      <Setter.Value> 
       <RotateTransform Angle="315" /> 
      </Setter.Value> 
      </Setter> 
     </Style> 
     </ComboBox.ItemContainerStyle> 
     <ComboBoxItem>Hello</ComboBoxItem> 
     <ComboBoxItem>World</ComboBoxItem> 
     <ComboBoxItem>Foo</ComboBoxItem> 
     <ComboBoxItem>Bar</ComboBoxItem> 
    </ComboBox> 
    </Grid> 
</Page> 

enter image description here

+0

嗯,这实际上更好,我要问如何让组合框完全垂直,只有45°的子项。很好! – 2011-04-17 15:51:03

0

您可能需要重写组合框的ItemTemplate这样的 -

<ComboBox Name="combo" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center"> 
     <ComboBox.LayoutTransform> 
      <RotateTransform Angle="270" /> 
     </ComboBox.LayoutTransform> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding Mode=OneWay}"> 
       <TextBox.LayoutTransform> 
        <RotateTransform Angle="315" /> 
       </TextBox.LayoutTransform> 
      </TextBox> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 
+0

我已经试过了,但随后表示45°空文本框。 – 2011-04-17 13:33:17

+0

您可以将代码粘贴到运行时填充文本框的位置吗?看着这可能会有所帮助.. – 2011-04-17 13:36:22

+0

我粘贴了代码。 – 2011-04-17 14:38:28

0

您可以使用DataTemplate并添加BindingTextBox.Text财产。现在我假设你想编辑名称,因为你正在使用TextBox。就目前而言,您的代码不支持编辑这些字符串,因为它们是不可变的。所以,我的回答让你想编辑的假设:

<ComboBox x:Name="combo" 
      Grid.Row="0" Grid.Column="1" 
      HorizontalAlignment="Center" 
      VerticalAlignment="Center"> 
    <ComboBox.Resources>   
     <DataTemplate DataType="{x:Type local:PlayerInfo}"> 
      <TextBox Text="{Binding Name}"> 
       <TextBox.LayoutTransform> 
        <RotateTransform Angle="270" /> 
       </TextBox.LayoutTransform> 
      </TextBox> 
     </DataTemplate> 
    </ComboBox.Resources> 
</ComboBox> 

而后面的代码应该设置ItemsSourcem_CurrentData.PlayersInfo

this.combo.ItemsSource = m_CurrentData.PlayersInfo; 

理想情况下,这可以通过XAML中的绑定来完成,但是这样做会起作用。请务必在您的XAML中设置xmlns:local定义,以便DataTemplate上的DataType起作用。

+0

请记住看到您编辑的反应式变化,您需要确保'PlayerInfo'类从'DependencyObject'继承或实现'INotifyPropertyChanged'。 – user7116 2011-04-17 14:55:48

+0

它不能解决我的问题,让组合框垂直站立完成。问题是从组合框显示45°角的项目,以便它更具可读性。 – 2011-04-17 15:27:05

+0

另外,关于绑定,我放弃了一阵子。我只是做旧的刷新。我的数据来自PHP/CLR,通过Phalanger,它存储在字典等。最后但并非最不重要的是,m_CurrentData对象在新序列化之前被销毁,所以即使PlayersInfo确实继承了DependencyObject,它也不会有机会通知任何事情,还是我错了? – 2011-04-17 15:34:37