WPF ComboBox - 垂直显示 45°旋转的项目
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有效(如果我正确理解你想要什么)
This works (if I correctly understood what you want)
您可以使用
DataTemplate
并将Binding
添加到TextBox.Text
属性。现在我假设您想要编辑名称,因为您正在使用TextBox
。按照目前的情况,您的代码将不支持编辑这些字符串,因为它们是不可变的。因此,我的回答假设您想要编辑它们:并且您后面的代码应将
ItemsSource
设置为m_CurrentData.PlayersInfo
。理想情况下,这可以通过 XAML 中的绑定来完成,但就目前情况而言,它是可行的。请务必在 XAML 中设置
xmlns:local
定义,以便DataTemplate
上的DataType
正常工作。You can use a
DataTemplate
and add aBinding
to theTextBox.Text
property. Now I'm assuming you want to edit the name because you are using aTextBox
. 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:And your code behind should set the
ItemsSource
tom_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 theDataType
on theDataTemplate
works.您可能需要像这样覆盖组合框项目模板 -
You might need to override the combobox itemtemplate like this -