[英]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。 嘗試通過SelectedItem
或SelectedIndex
屬性處理組合框本身中的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.