簡體   English   中英

WPF組合框通過鼠標選擇項目

[英]Wpf combobox selected item by mouse

我有一個文本對齊按鈕組合框,當我嘗試通過鼠標單擊選擇其中一個時,所選項目不會更改。 似乎ComboBoxItem沒有收到Click事件。 有人知道如何處理嗎?

<ComboBox x:Name="FontAligmentCombo">
  <ComboBoxItem IsSelected="True">
    <Button Command="EditingCommands.AlignLeft"
            ToolTip="Align Left">
        <Viewbox>
            <ContentPresenter Content="{DynamicResource icon8_Win10_AlignLeft}" />
        </Viewbox>
    </Button>
  </ComboBoxItem>
  <ComboBoxItem>
    <Button Command="EditingCommands.AlignCenter"
            ToolTip="Align Center">
        <Viewbox>
            <ContentPresenter Content="{DynamicResource icon8_Win10_AlignCenter}" />
        </Viewbox>
    </Button>
  </ComboBoxItem>
</ComboBox>
<ComboBoxItem IsSelected="True">
<Button Command="EditingCommands.AlignLeft"
        ToolTip="Align Left">
    <Viewbox>
        <ContentPresenter Content="{DynamicResource icon8_Win10_AlignLeft}" />
    </Viewbox>
</Button>

您將IsSelected硬編碼為True。 嘗試通過SelectedItemSelectedIndex屬性處理組合框本身中的SelectedItem項目。 就您而言,索引是有意義的。 如果不能,則應綁定布爾值。

Button “吞下”點擊。 您將必須以編程方式選擇相應的ComboBoxItem

嘗試這個:

<ComboBox x:Name="FontAligmentCombo">
    <ComboBox.Resources>
        <Style TargetType="Button">
            <EventSetter Event="Click" Handler="OnClick" />
        </Style>
    </ComboBox.Resources>
    <ComboBoxItem IsSelected="True">
        <Button Command="EditingCommands.AlignLeft" ToolTip="Align Left">
            <Viewbox>
                <ContentPresenter Content="{DynamicResource icon8_Win10_AlignLeft}" />
            </Viewbox>
        </Button>
    </ComboBoxItem>
    <ComboBoxItem>
        <Button Command="EditingCommands.AlignCenter" ToolTip="Align Center">
            <Viewbox>
                <ContentPresenter Content="{DynamicResource icon8_Win10_AlignCenter}" />
            </Viewbox>
        </Button>
    </ComboBoxItem>
</ComboBox>

private void OnClick(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    ComboBoxItem cbi = FindParent<ComboBoxItem>(button);
    if (cbi != null)
    {
        FontAligmentCombo.SelectedItem = cbi;
        FontAligmentCombo.IsDropDownOpen = false;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM