WPF ComboBox - 垂直显示 45°旋转的项目

发布于 2024-11-02 01:54:43 字数 1404 浏览 2 评论 0原文

我想在 WPF 中使用组合框来存储一些名称,但我希望组合框本身是垂直的,当您单击它时,它应以另一个 45° 旋转显示每个项目,以便更容易阅读。像这样的东西:l / / / /

我这样做实现了其中一些:

                        <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);                    
        }

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

编辑:当我清除项目时,不再有空白项目。

I want to use a ComboBox in WPF to store some names, but I want the combobox itself to be vertical, and when you click on it, it shall display every item with another 45° rotation so that it's more readable. Something like: l / / / /

I achieved some of it doing this:

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

I fill the combobox like this:

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

But only the first item gets rotated, plus i get a blank item on top of the actual items (I fill the combobox items at runtime).
What am I doing wrong?

EDIT: no more blank item as i clear the items.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红墙和绿瓦 2024-11-09 01:54:43

这有效(如果我正确理解你想要什么)

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

在此处输入图像描述

This works (if I correctly understood what you want)

<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

明天过后 2024-11-09 01:54:43

您可以使用DataTemplate 并将Binding 添加到TextBox.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>

并且您后面的代码应将 ItemsSource 设置为 m_CurrentData.PlayersInfo

this.combo.ItemsSource = m_CurrentData.PlayersInfo;

理想情况下,这可以通过 XAML 中的绑定来完成,但就目前情况而言,它是可行的。请务必在 XAML 中设置 xmlns:local 定义,以便 DataTemplate 上的 DataType 正常工作。

You can use a DataTemplate and add a Binding to the TextBox.Text property. Now I'm assuming you want to edit the name because you are using a TextBox. As it stands your code will not support editing those strings, because they are immutable. So my answer makes the assumption you'd like to edit them:

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

And your code behind should set the ItemsSource to m_CurrentData.PlayersInfo.

this.combo.ItemsSource = m_CurrentData.PlayersInfo;

Ideally this would be done through bindings in the XAML, but as this stands it will work. Be sure to set up the xmlns:local definition in your XAML so the DataType on the DataTemplate works.

很糊涂小朋友 2024-11-09 01:54:43

您可能需要像这样覆盖组合框项目模板 -

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

You might need to override the combobox itemtemplate like this -

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文