我有一组按钮,它们应该像切换按钮一样,但也可以作为单选按钮,在当前时间只能选择/按下一个按钮.它还需要具有不选择/按下任何按钮的状态.
行为将有点像Photoshop工具栏,其中零或一个工具随时被选中!
知道如何在WPF中实现这一点吗?
小智 290
在我看来,这是最简单的方法.
<RadioButton Style="{StaticResource {x:Type ToggleButton}}" />
Run Code Online (Sandbox Code Playgroud)
请享用! - Pricksaw
Bry*_*son 39
最简单的方法是设置ListBox的样式,以便为其ItemTemplate使用ToggleButtons
<Style TargetType="{x:Type ListBox}">
<Setter Property="ListBox.ItemTemplate">
<Setter.Value>
<DataTemplate>
<ToggleButton Content="{Binding}"
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用ListBox的SelectionMode属性来处理SingleSelect与MultiSelect.
RoK*_*oKK 31
<RadioButton Content="Point" >
<RadioButton.Template>
<ControlTemplate>
<ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
</ControlTemplate>
</RadioButton.Template>
</RadioButton>
Run Code Online (Sandbox Code Playgroud)
它适合我,享受!
你总是可以在ToggleButton的Click上使用一个泛型事件,它在VisualTreeHelper的帮助下将groupcontrol(Grid,WrapPanel,...)中的所有ToggleButton.IsChecked设置为false; 然后重新检查发件人.或类似的东西.
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
int childAmount = VisualTreeHelper.GetChildrenCount((sender as ToggleButton).Parent);
ToggleButton tb;
for (int i = 0; i < childAmount; i++)
{
tb = null;
tb = VisualTreeHelper.GetChild((sender as ToggleButton).Parent, i) as ToggleButton;
if (tb != null)
tb.IsChecked = false;
}
(sender as ToggleButton).IsChecked = true;
}
Run Code Online (Sandbox Code Playgroud)