Laden...

Nur ein Button von mehren aktiv

Letzter Beitrag vor einem Monat 3 Posts 243 Views
Nur ein Button von mehren aktiv

Hallo alle!

Gibt es eine Möglichkeit von mehreren Buttons nur einen aktiv zu lassen? Ich will die Buttons für die Auswahl des Schwierigkeitsgrads erstellen und deshalb nur erlauben, dass einer aktiv ist. RadioButtons gefallen mir vom Aussehen nicht und deswegen habe ich mich gewundert, ob ihr vielleicht mehr wisst.

Mit besten Grüßen!

Radiobuttons kann man auch stylen. Du hast also die Möglichkeit, das Aussehen nach Deinen Wünschen anzupassen.

Du kannst auch separate Buttons benutzen und eine Logik entwerfen, jeden Button über die Eigenschaft IsEnabled zu aktivieren/deaktivieren.

Das wäre eine Möglichkeit

<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow"
    Width="800"
    Height="450"
    d:DataContext="{d:DesignData IsDesignTimeCreatable=False,
                                 Type={x:Type local:MainWindowModel}}"
    mc:Ignorable="d">
    <Window.DataContext>
        <local:MainWindowModel />
    </Window.DataContext>
    <Window.Resources>
        <ObjectDataProvider
            x:Key="Difficulties"
            MethodName="GetValues"
            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:Difficulty" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <DockPanel>
        <StackPanel DockPanel.Dock="Top">
            <ListBox ItemsSource="{Binding Source={StaticResource Difficulties}}" SelectedItem="{Binding SelectedDifficulty}">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Padding" Value="0" />
                        <Setter Property="Margin" Value="0" />
                        <Setter Property="Focusable" Value="False" />
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Button
                            MinWidth="80"
                            Margin="0"
                            Padding="5,2"
                            Command="{Binding DataContext.SelectDifficultyCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                            CommandParameter="{Binding}"
                            Content="{Binding}">
                            <Button.Style>
                                <Style TargetType="{x:Type Button}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                                            <Setter Property="Background" Value="Red" />
                                            <Setter Property="Foreground" Value="White" />
                                            <Setter Property="IsHitTestVisible" Value="False" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Button.Style>
                        </Button>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
        <Grid />
    </DockPanel>
</Window>
public enum Difficulty
{
    Easy,
    Medium,
    Hard
}

public class MainWindowModel : ReactiveObject
{
    [Reactive]
    public Difficulty SelectedDifficulty { get; set; }
    public ReactiveCommand<Difficulty, Unit> SelectDifficultyCommand { get; }

    public MainWindowModel()
    {
        SelectDifficultyCommand = ReactiveCommand.Create<Difficulty>( SelectDifficultyCommandExecute );
    }

    private void SelectDifficultyCommandExecute( Difficulty d )
    {
        SelectedDifficulty = d;
    }
}

Möglicherweise ist aber auch der ToggleButton etwas für dich

            <ListBox ItemsSource="{Binding Source={StaticResource Difficulties}}"
                     SelectedItem="{Binding SelectedDifficulty}">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Padding"
                                Value="0" />
                        <Setter Property="Margin"
                                Value="0" />
                        <Setter Property="Focusable"
                                Value="False" />
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ToggleButton MinWidth="80"
                                     Margin="0"
                                     Padding="5,2"
                                     Content="{Binding}"
                                     IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Hat die Blume einen Knick, war der Schmetterling zu dick.