0

I have a multiselect ComboBox with the following template:

 <ComboBox.ItemTemplate>
        <DataTemplate>
              <StackPanel Orientation="Horizontal">
                   <CheckBox
                         VerticalAlignment="Center"
                         Checked="checkBox_Checked"
                         ClickMode="Press"
                         Unchecked="checkBox_Unchecked" />
                   <TextBlock Text="{Binding Position}" />
              </StackPanel>
        </DataTemplate>
 </ComboBox.ItemTemplate>

My question is, if the checkBox is checked, how could I get the value from the TextBlock (what should be the code in checkBox_Checked? I will be needing it for further use.

Raya
  • 148
  • 1
  • 10

1 Answers1

2

There shouldn't be any Checked or Unchecked event handler.

Instead, the view model (that exposes the Position property) should expose another property, to which you bind the IsChecked property of the CheckBox:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding Checked}" />
        <TextBlock Text="{Binding Position}" />
    </StackPanel>
</DataTemplate>

In the setter of the Checked property you can access the Position property.

Also be aware that the TextBlock in the ItemTemplate seems to be redundant:

<DataTemplate>
    <CheckBox IsChecked="{Binding Checked}"
              Content="{Binding Position}" />
</DataTemplate>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thank you, I have tried this approach, but I don't know how to proceed. Should an event be called after Checked turns true? – Raya Sep 23 '21 at 07:54
  • That depends on what you are doing. You seem to be asking how to access Position when the CheckBox is checked or unchecked. Do that in the setter of the Checked property of the view model class, i.e. the same class that has the Position property. – Clemens Sep 23 '21 at 09:24
  • What I want to do is add a column dynamically to the DataGrid with a header whose value is equal to Position. I am just splitting the problem in parts and figuring them out one by one. – Raya Sep 23 '21 at 20:25
  • What does that have to do with "*get[ting] the value from the TextBlock*" in your question? That would be a totally different task. – Clemens Sep 23 '21 at 20:28
  • Well, I need the value of `Position` in this case, in order to put it in the column header. I might be going in a completely different direction, I'm still new in WPF, sorry. – Raya Sep 24 '21 at 06:15
  • Maybe, but that still hasn't got anything to do with your question. The answer to your question is here. If you have a different task, ask another question. – Clemens Sep 24 '21 at 06:24
  • I asked this question -> https://stackoverflow.com/questions/69265943/dynamically-add-a-column-in-wpf-datagrid but the answer didn't work for me. – Raya Sep 24 '21 at 06:40
  • And what does it have to do with this one? As said, the answer to this question is here. – Clemens Sep 24 '21 at 06:53
  • I meant that I asked the full question there, the answer to this question is correct. – Raya Sep 24 '21 at 06:57