使用 SelectedIndex 或导航键盘时,ComboBoxItem 是否为 ComboBox 的全宽?

发布于 2024-08-08 06:35:29 字数 606 浏览 3 评论 0原文

遵循 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 技术交流群。

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

发布评论

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

评论(2

转身以后 2024-08-15 06:35:29

您只需将边框的宽度设置为外部控件的动态宽度:

例如

Width="{Binding ElementName=combox1, Path=ActualWidth}">

试试这个:

<ComboBox x:Name="combox1" Height="30" Width="300" HorizontalContentAlignment="Stretch" 
    SelectedIndex="0">
    <ComboBoxItem HorizontalContentAlignment="Stretch">
        <Border Background="Red" Width="{Binding ElementName=combox1, Path=ActualWidth}">
            <TextBlock>My stuff...</TextBlock>
        </Border>
    </ComboBoxItem>
</ComboBox>

You just need to set the width of your border to the dynamic width of your outercontrol:

E.g.

Width="{Binding ElementName=combox1, Path=ActualWidth}">

Try this:

<ComboBox x:Name="combox1" Height="30" Width="300" HorizontalContentAlignment="Stretch" 
    SelectedIndex="0">
    <ComboBoxItem HorizontalContentAlignment="Stretch">
        <Border Background="Red" Width="{Binding ElementName=combox1, Path=ActualWidth}">
            <TextBlock>My stuff...</TextBlock>
        </Border>
    </ComboBoxItem>
</ComboBox>
饮惑 2024-08-15 06:35:29

我明白了 - 我确信有办法解决这个问题。这实际上取决于您想要的最终结果。您的每个数据项是否都有不同的背景颜色来识别它们,或者它只是您想要实现的整个组合框的背景颜色。

如果是后者,请尝试此操作 - 也许也删除突出显示的选择颜色,否则就预选择第一个项目而言,路线后面的代码可能是正确的,因为这可能是一个选项。

整体背景颜色示例:

    <ComboBox Background="Red" x:Name="combox2" Height="30" HorizontalContentAlignment="Stretch" SelectedIndex="0">
        <ComboBoxItem Background="Red" HorizontalContentAlignment="Stretch">
            <TextBlock Background="Red">My stuff...</TextBlock>
        </ComboBoxItem>
    </ComboBox>

希望这有帮助! :)

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:

    <ComboBox Background="Red" x:Name="combox2" Height="30" HorizontalContentAlignment="Stretch" SelectedIndex="0">
        <ComboBoxItem Background="Red" HorizontalContentAlignment="Stretch">
            <TextBlock Background="Red">My stuff...</TextBlock>
        </ComboBoxItem>
    </ComboBox>

Hope this helps! :)

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