我有一个文本对齐按钮的组合框,当我尝试通过鼠标点击选择其中一个按钮时,所选项目不会改变。似乎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>
答案 0 :(得分:0)
<ComboBoxItem IsSelected="True">
<Button Command="EditingCommands.AlignLeft"
ToolTip="Align Left">
<Viewbox>
<ContentPresenter Content="{DynamicResource icon8_Win10_AlignLeft}" />
</Viewbox>
</Button>
您将IsSelected
硬编码为True。尝试通过SelectedItem
或SelectedIndex
属性处理Combobox中的所选项目。在你的情况下索引是有道理的。如果你不能,你应该绑定bool值。
答案 1 :(得分:0)
Button
&#34;燕子&#34;点击。您必须以编程方式选择相应的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;
}
}