我正在尝试复制 Word 中的左/中/右对齐工具栏按钮。当您单击“左对齐”按钮时,中心和右按钮取消选中。我正在使用带有 ToggleButtons 的 WPF 列表框。
问题是用户可以单击左对齐按钮两次。第二次单击会使按钮取消选中并将基础值设置为空。我希望第二次单击什么也不做。
想法?强制列表框总是有一个选定的项目?防止视图模型中的 null (需要刷新 ToggleButton 绑定)?
<ListBox ItemsSource="{x:Static domain:FieldAlignment.All}" SelectedValue="{Binding Focused.FieldAlignment}">
<ListBox.ItemTemplate>
<DataTemplate>
<ToggleButton IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}">
<TextBlock Text="{Binding Description}" />
</ToggleButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>