使用 SelectedIndex 或导航键盘时,ComboBoxItem 是否为 ComboBox 的全宽?
遵循 XAML 会导致“我的东西”以 ComboBox 为中心,直到我打开 ComboBox,此时它会正确拉伸自身。
<ComboBox Height="30" Width="300" HorizontalContentAlignment="Stretch" SelectedIndex="0">
<ComboBoxItem HorizontalContentAlignment="Stretch">
<Border Background="Red">
<TextBlock>My stuff...</TextBlock>
</Border>
</ComboBoxItem>
</ComboBox>
问题是,即使使用 SelectedIndex
选择 ComboBoxItem 是否也可能被拉伸?如果 SelectedIndex 未受影响 (-1) 并且使用键盘选择项目,则会发生相同的错误或功能。
解决方法可能是在应用程序的开头以编程方式打开组合框,这相当难看。
Following XAML causes the "My stuff" being centered of ComboBox
until I open the ComboBox
, when it correctly stretches itself.
<ComboBox Height="30" Width="300" HorizontalContentAlignment="Stretch" SelectedIndex="0">
<ComboBoxItem HorizontalContentAlignment="Stretch">
<Border Background="Red">
<TextBlock>My stuff...</TextBlock>
</Border>
</ComboBoxItem>
</ComboBox>
The question is, is it possible to get the ComboBoxItem
being stretched even when it's selected using SelectedIndex
? Same bug, or feature, happens if SelectedIndex is untouched (-1) and one selects the item using keyboard.
Workaround is probably to open the ComboBox programmatically in the beginning of app, which is rather ugly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需将边框的宽度设置为外部控件的动态宽度:
例如
试试这个:
You just need to set the width of your border to the dynamic width of your outercontrol:
E.g.
Try this:
我明白了 - 我确信有办法解决这个问题。这实际上取决于您想要的最终结果。您的每个数据项是否都有不同的背景颜色来识别它们,或者它只是您想要实现的整个组合框的背景颜色。
如果是后者,请尝试此操作 - 也许也删除突出显示的选择颜色,否则就预选择第一个项目而言,路线后面的代码可能是正确的,因为这可能是一个选项。
整体背景颜色示例:
希望这有帮助! :)
I see yes - i'm sure there is a way round this. It really depends what the end result is that you want. Do each of your data items have a difference background colour to identify them or is it simply a background colour to your whole combobox which you are trying to achieve.
If it is the latter, try this - and perhaps also remove the highlighted selection colour too, else perhaps the code behind route is correct, in terms of preselecting your first item, as that may be an option.
Example of All over Background colour:
Hope this helps! :)