ToggleButton组:确保始终在ListBox中选择一个项目

Bri*_*Low 5 wpf listbox togglebutton

我试图复制Word中的左/中/右对齐工具栏按钮.单击"左对齐"按钮时,取消选中"中心"和"右"按钮.我正在使用带有ToggleButtons的WPF ListBox.

问题是用户可以单击两次左对齐按钮.第二次单击会导致按钮取消选中并将基础值设置为null.我想第二次点击什么都不做.

想法?强制ListBox始终有一个选定的项目?防止视图模型中的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>
Run Code Online (Sandbox Code Playgroud)

dnr*_*nr3 4

是的,对于这种情况,我也更喜欢单选按钮,但是如果您想使用切换按钮,那么也许您可以将 isenabled 属性绑定到 ischecked ,这样在检查时就无法单击它