简体   繁体   中英

WPF Radio Button Group How to enable a TextBox when any item gets selected?

I have no issues getting this functionality to work with textboxes and comboboxes by simply binding the IsEnabled Property of the control I want to stay hidden until something is selected to the .Text Property, like so:

<Label Content="Please Select the status for this client:" Margin="0,0,65,0" IsEnabled="{Binding CBCustomer.Text}" />

So basically this item will not show until someone selects something in the ComboBox.

How do I get the same functionality to work with a group of radio buttons? Basically I have a stackpanel that I want the IsEnabled property(shown as XXXXXX) to be bound to a Boolean value of whether any button was selected.

  <StackPanel Orientation="Horizontal" Canvas.Left="54"  Canvas.Top="40" Margin="0,0,0,50">
            <Label Content="Please Select the RAG Status for this client:" Margin="0,0,65,0" IsEnabled="{Binding CBCustomer.Text}" />
            <RadioButton GroupName="RAGGroup" Margin="0,8,0,0" Background="Red" Foreground="Red" IsEnabled="{Binding CBCustomer.Text}">Red</RadioButton>
            <RadioButton GroupName="RAGGroup" Margin="10,8,0,0" Background="DarkGoldenrod" Foreground="DarkGoldenrod" IsEnabled="{Binding CBCustomer.Text}">Amber</RadioButton>
            <RadioButton GroupName="RAGGroup" Margin="10,8,0,0" Background="Green" Foreground="Green" IsEnabled="{Binding CBCustomer.Text}">Green</RadioButton>
        </StackPanel>

  <StackPanel Margin="55,80,0,0" IsEnabled="{Binding XXXXXXX}">

Use a Multi-Binding? I see examples that show how to get the value back from the button that was selected but I don't care about that, I simply need to see whether the value was selected or not.

If you want to bind to other elements, you can use DataTriggers :

<RadioButton x:Name="RB1"/>
<RadioButton x:Name="RB2"/>
<RadioButton x:Name="RB3"/>
<StackPanel>
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=RB1, Path=IsChecked}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=RB2, Path=IsChecked}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=RB3, Path=IsChecked}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
</StackPanel>

But I almost always bind to the data layer, like:

<RadioButton IsChecked="{Binding RAG, Converter=...}"/>
<RadioButton IsChecked="{Binding RAG, Converter=...}"/>
<RadioButton IsChecked="{Binding RAG, Converter=...}"/>
<StackPanel>
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Setter Property="IsEnabled" Value="True"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RAG}" Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
</StackPanel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM