1

Hi I have a WPF application, with a WPF datagrid with an observable collection of objects. Each object in the observable collection has a boolean variable that is represented by the check box in the data grid. What I am trying to do is make it so that when I check the box and make it "true" for one object of the collection, it makes the other objects of the collection be set to "false". In other words, when I click my data grid checkbox for one object, it unchecks the check box for the other objects in the collection. Its such a simple concept, but I have tried several ways to do this including using a property, but I get stack overflow cause it keeps looping through the collection forever, I tried using an event related to the clicking of the checkbox but I could not get the event to make all other objects in the data grid to be set to false because I couldn't figure out how to access the items in the datagrid to modify them. There must be a really easy way to do this that I am overlooking.

the WPF code it here:

<DataGrid x:Name="DataGrid1" AutoGenerateColumns="False"     ItemsSource="{Binding CollectionOfThings}" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" SelectionMode="Single" Margin="0,0,0,124">

<DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Column with a checkbox" Binding= "{Binding CheckBoxBooleanValue}" />

</DataGrid.Columns>

I have the c# code:

The collection contains objects from this simple class:

public class Thing
{
public bool CheckBoxBooleanValue {get; set;}
}

My main Window constructor in my WPF form creates the collection when the application starts:

public MainWindow()
{
ObservableCollection<Thing> CollectionOfThings;
Thing thing1 = new Thing();
CollectionOfThings.Add(thing1);
Thing thing2 = new Thing();
CollectionOfThings.Add(thing2);
}

1 Answer 1

1

Basically you need a Radio button functionality. If you have radio buttons that follow the same group, then when you set one to true, others will be set to false. Unfortunately, you can't add a radio button column in your data grid in a straight forward manner. A small trick will do the job for you and I'm pasting the code for that.

NOTE: When I set the group name to "myGroup", this ensures that all radio buttons follow the same group and your desired functionality is met.

 <DataGrid Name="data" AutoGenerateColumns="False"  
              ItemsSource="{Binding CollectionOfThings}" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" SelectionMode="Single" Margin="0,0,0,124">

        <DataGrid.Columns>
            <DataGridTemplateColumn  >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <RadioButton IsChecked= "{Binding CheckBoxBooleanValue}" GroupName="myGroup" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks soooo much! I completely forgot about using the radiobutton control!

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.