Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Binding Textbox and RadioButton
Posted
Updated 23-Apr-12 7:50am
v2

If you just want the RadioButton checked when the textbox has focus, this works too:
<radiobutton ischecked="{Binding IsFocused, ElementName=txtBox, Mode=OneWay}">
    <textbox x:name="txtBox" xmlns:x="#unknown" />
</radiobutton>
 
Share this answer
 

In order to set the same group to some RadioButtons, you can set the GroupName property of the RadioButtons to the same value. For enabling a TextBox according to the RadioButton's state, you can use the ElementName property of the Binding. For instance, see the following StackPanel:


XML
<StackPanel>
    <StackPanel Orientation="Horizontal" Margin="2">
        <RadioButton Name="op1" Content="Option 1: " GroupName="A" />
        <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op1}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="2">
        <RadioButton Name="op2" Content="Option 2: " GroupName="A" />
        <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op2}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="2">
        <RadioButton Name="op3" Content="Option 3: " GroupName="A" />
        <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op3}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="2">
        <RadioButton Name="op4" Content="Option 4: " GroupName="A" />
        <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op4}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal" Margin="2">
        <RadioButton Name="op5" Content="Option 5: " GroupName="A" />
        <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op5}" />
    </StackPanel>
</StackPanel>

Edited:


If you want the RadioButton to be checked when you click on the disabled TextBox, you can set the TextBox as a part of the Content of the RadioButton, like in the following StackPanel:


XML
<StackPanel>
    <RadioButton Name="op1" GroupName="A" Margin="2">
        <RadioButton.Content>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Option 1: " />
                <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op1}" />
            </StackPanel>
        </RadioButton.Content>
    </RadioButton>
    <RadioButton Name="op2" GroupName="A" Margin="2">
        <RadioButton.Content>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Option 2: " />
                <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op2}" />
            </StackPanel>
        </RadioButton.Content>
    </RadioButton>
    <RadioButton Name="op3" GroupName="A" Margin="2">
        <RadioButton.Content>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Option 3: " />
                <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op3}" />
            </StackPanel>
        </RadioButton.Content>
    </RadioButton>
    <RadioButton Name="op4" GroupName="A" Margin="2">
        <RadioButton.Content>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Option 4: " />
                <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op4}" />
            </StackPanel>
        </RadioButton.Content>
    </RadioButton>
    <RadioButton Name="op5" GroupName="A" Margin="2">
        <RadioButton.Content>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Option 5: " />
                <TextBox Width="200" IsEnabled="{Binding IsChecked, ElementName=op5}" />
            </StackPanel>
        </RadioButton.Content>
    </RadioButton>
</StackPanel>
 
Share this answer
 
v2
Comments
Shmuel Zang 22-Apr-12 7:53am    
See the edited solution.
Shmuel Zang 22-Apr-12 11:10am    
That can be done using code, by calling the Focus method of the TextBox in the event-handler of its IsEnabledChanged event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900