[英]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.