3

I want to restyle a WPF ComboBox which is formatted to be a drop-list type, BUT remove the selected TextBox which gets populated with the selected contents and just replace it with some static text and an image which remains constant, simulating a button like look.

So in effect it becomes a button-drop-list, so when I select an item from the drop list, I can populate another control via command bindings with its selected value and the button style remains.

Basically something like this crude picture I've hacked together.

button drop-list

I've seen examples of button with context menus, but I don't like the idea, and a ComboBox fits my needs perfectly in terms of function and easy command and data binding.

I know it can be done, but I lost faith in my ablity after reading overly confusing examples based on other controls. I couldn't find an example detailing my needs to learn from.

Cheers DIGGIDY

4

3 回答 3

1

在玩了很多之后,我决定更好的选择是使用带有绑定上下文菜单的按钮,这最终成为更好的解决方案。

谢谢你的帮助马克。

于 2013-05-29T13:04:47.253 回答
0

我遇到了同样的问题,实际上,这很简单。只需放置一个带有 SelectionChanged 事件的只读 ComboBox。您将静态文本放入索引 0。

现在,当用户选择某些东西时,获取选定的项目,然后将 SelectedIndex 设置为 0。所以你得到了用户选择的项目,但显示的文本是相同的。

看:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox combo = (ComboBox)sender;
    if (combo.SelectedIndex > 0)
    {
        // Do your stuff here...

        // Then
        combo.SelectedIndex = 0;
    }
}
于 2013-05-22T11:12:16.890 回答
0

[编辑]据我说,我更喜欢我以前的答案。因此,请确保您,读者,我之前的回答与您的期望不符。[/编辑]

另一个答案是将您的对象放在 ComboBox 上方,然后从该对象捕获 MouseDown 事件并放下 ComboBox。我在示例中使用了只读文本框。

看:

<Grid>
    <ComboBox x:Name="Combo" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120">
        <ComboBoxItem Content="TEST" />
        <ComboBoxItem Content="TEST1" />
        <ComboBoxItem Content="TEST2" />
        <ComboBoxItem Content="TEST3" />
    </ComboBox>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" Text="TextBox" VerticalAlignment="Top" Width="120" IsReadOnly="True" PreviewMouseDown="TextBox_PreviewMouseDown"/>
</Grid>

然后是后面的代码:

private void TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true; // Prevents the event.
    Combo.IsDropDownOpen = true; // Drops down the ComboBox.
}

这对我来说可以。

于 2013-05-22T12:32:26.897 回答