1

I've a listview containing name and two checkboxes( name, male(checkbox),female(checkbox)). I want make sure only one checkbox is selected from listview at a time.

   <UserControl.Resources>
   <DataTemplate x:Key="datatemp">
        <StackPanel Orientation="Horizontal" Width="200" >
            <TextBlock Text="{Binding VmName}" Width="129" Visibility="Visible" />
            <CheckBox  Name="cb"  IsThreeState="False" Checked="off_chek_select"  IsChecked="{Binding IsCheck, Mode=TwoWay}"  Margin="6,0,18,6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            <CheckBox  Name="cb1" IsThreeState="False" Checked="ins_chek_select" IsChecked="{Binding IsCheck1, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

here, I've used this datatemplate in listview. I'm not able to access controls from datatemplate for writing on check event.

1 Answer 1

0

Into Tag property you can put reference to parent element (in your case it's StackPanel) and in code-behind you can find element by using FindName method.

XAML:

<DataTemplate x:Key="datatemp">
    <StackPanel x:Name="stackPanel" Orientation="Horizontal" Width="200" >
        <TextBlock Text="{Binding VmName}" Width="129" Visibility="Visible" />
        <CheckBox  Name="cb" Tag="{Binding ElementName=stackPanel}"  IsThreeState="False" Checked="off_chek_select"  IsChecked="{Binding IsCheck, Mode=TwoWay}"  Margin="6,0,18,6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        <CheckBox  Name="cb1" Tag="{Binding ElementName=stackPanel}" IsThreeState="False" Checked="ins_chek_select" IsChecked="{Binding IsCheck1, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />
    </StackPanel>
</DataTemplate>

Code-behind:

private void off_chek_select(object sender, RoutedEventArgs e)
{
    var cbSender = sender as CheckBox;
    if (cbSender != null)
    {
        var stackPanel = cbSender.Tag as StackPanel;
        if (stackPanel != null)
        {
            var cb1 = stackPanel.FindName("cb1") as CheckBox;
            if (cb1 != null)
            {
                cb1.IsChecked = !cbSender.IsChecked;
            }
        }
    }
}

private void ins_chek_select(object sender, RoutedEventArgs e)
{
    var cbSender = sender as CheckBox;
    if (cbSender != null)
    {
        var stackPanel = cbSender.Tag as StackPanel;
        if (stackPanel != null)
        {
            var cb = stackPanel.FindName("cb") as CheckBox;
            if (cb != null)
            {
                cb.IsChecked = !cbSender.IsChecked;
            }
        }
    }
}

If you have multiple choices and only one should be selected at the time you could use RadioButton (msdn).

<UserControl.Resources>
    <DataTemplate x:Key="datatemp">
        <StackPanel Orientation="Horizontal" Width="200" >
            <TextBlock Text="{Binding VmName}" Width="129" Visibility="Visible" />
            <RadioButton  Name="cb"  IsThreeState="False" IsChecked="{Binding IsCheck, Mode=TwoWay}"  Margin="6,0,18,6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            <RadioButton  Name="cb1" IsThreeState="False" IsChecked="{Binding IsCheck1, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>
Sign up to request clarification or add additional context in comments.

4 Comments

actually it is an example of what i am trying to do. I dont want to use radiobuttons. Is there anyway to do it with Chekboxes
@AbhishekNehe check my answer again :)
i cant access the second checkbox on click event of first checkbox. i want to implement same functionality like radioboxes but using checkboxes.
@AbhishekNehe check my answer again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.