-1

I have such a listbox like below:

    <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="385" Margin="21,138,0,0" VerticalAlignment="Top" Width="273" ItemsSource="{Binding Path=locationList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Button Name="btnDelete" Click="btnDelete_Click" Width="15" Height="15" HorizontalAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Content="x" />
                    <CheckBox Name="checkBox" />
                    <TextBlock Name="textBox" Text="{Binding}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

And what I would like to do is to set checkbox for specific item this listbox.

I am trying to do it:

    private void button4_Click(object sender, RoutedEventArgs e)
    {
        for(int i = 0; i < listBox.Items.Count; i++)
        {
            listBox.Items[i].checkBox = false;
        }
    }

I know I am doing an error. I would like to cast it to object of item and then set an item's property (this checkbox) to false. May anyone correct me ? Thank you in advance.

edit:

Before I was trying to do it this way:

        foreach (var item in listBox.SelectedItems)
        {
            item.
        }

but all possibilities I have got are just standard methods: Equals, GetHashCode, GetType, ToString... How I may refer to checkbox ?

Moreover I will supply my question with the insight. I would like to find a specific item by text which is in line in listbox (item) and then change checkbox for this item (same row in listbox). Second logic to be implemented is to set all rows to selected or unselected (this is what I am trying to do now).

Thank you for response.

jan kowalski
  • 189
  • 3
  • 21
  • ListBox you should change your for loop to use foreach for example `foreach(var item in listBox.SelectedItems)` something like that.. – MethodMan Jun 06 '16 at 21:29
  • Thank you for time :) I have edited post as the answer for you. – jan kowalski Jun 06 '16 at 21:37
  • http://www.c-sharpcorner.com/forums/how-to-uncheck-checked-items-in-a-checkedlistbox – MethodMan Jun 06 '16 at 21:47
  • Unfortunatelly this is not solving my problem since it is for windows forms and I am using wpf and I do not have such a property as in article. In general I have listbox standard, which doesnt see the checklist or something like that. I have shown construction of listbox above in post. Regards – jan kowalski Jun 06 '16 at 22:10
  • do a simple google search come on google the following `how to set CheckedListBox items to unchecked C# WPF` – MethodMan Jun 06 '16 at 22:12
  • The problem is that I have not CheckListBox or any model for this just List is my ItemSource. Then it is hard to find solution on google. I have tried it. Thank you for time. – jan kowalski Jun 06 '16 at 23:15

1 Answers1

3
        for (int i = 0; i < listBox.Items.Count; i++)
        {
            var item = listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items[i]) as ListBoxItem;
            var template = item.ContentTemplate as DataTemplate;

            ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(item);

            CheckBox myCheckBox = (CheckBox)template.FindName("checkBox", myContentPresenter);

            myCheckBox.IsChecked = true;
        }

Likewise you can find the TextBlock with (note, you named it "textBox" not "textBlock")

            TextBlock myTextBlock = (TextBlock)template.FindName("textBox", myContentPresenter);

FindVisualChild can be found here FindVisualChild reference issue

Community
  • 1
  • 1
user2880486
  • 1,068
  • 7
  • 16
  • Hey, this is working like a charm :) Until 21th item which is always causing error of the message: "Object reference is not set to an instance of an object.". As my itemSource I have List. The 21 item is not special, doesn't have special characters or anything. What I am thinking to write a little model for this, a class containing my string and bool or make it as List>. By the way do you have an idea why this exception is occuring ? Your proposal is great anyway. It is explaining me some wpf complexity. – jan kowalski Jun 06 '16 at 23:23
  • 1
    @jankowalski The problem is the 21st item has not been created. You can cause it to be created by scrolling to the last item. Or add this to your ListBox VirtualizingPanel.IsVirtualizing="False". – user2880486 Jun 07 '16 at 14:39
  • I am impressed by your knowledge of WPF. I wish you all good things man. Thank you very much for helping me to solve this problems. – jan kowalski Jun 07 '16 at 22:22