0

I m changing TextBlock element's IsEnabled prop of listbox as below

    <ListBox Name="myListBox" ItemTemplate="{StaticResource myDataTemplate}"
     IsSynchronizedWithCurrentItem="True">
      <ListBox.ItemsSource>
      <Binding Source="{StaticResource InventoryData}" XPath="Books/Book"/>
      </ListBox.ItemsSource>
   </ListBox>

The ListBox uses the following DataTemplate as

   <DataTemplate x:Key="myDataTemplate">
      <TextBlock Name="textBlock" FontSize="14" Foreground="Blue">
      <TextBlock.Text>
     <Binding XPath="Title"/>
     </TextBlock.Text>
     </TextBlock>
   </DataTemplate>


 // Getting the currently selected ListBoxItem
 // Note that the ListBox must have
 // IsSynchronizedWithCurrentItem set to True for this to work
   ListBoxItem myListBoxItem =
  (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

 // Getting the ContentPresenter of myListBoxItem
 ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

 // Finding textBlock from the DataTemplate that is set on that ContentPresenter
    DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
   TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
   // Do something to the DataTemplate-generated TextBlock
   myTextBlock.IsEnabled=false;


   private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
   {
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
      {
      DependencyObject child = VisualTreeHelper.GetChild(obj, i);
      if (child != null && child is childItem)
        return (childItem)child;
       else
       {
        childItem childOfChild = FindVisualChild<childItem>(child);
        if (childOfChild != null)
            return childOfChild;
        }
      }
       return null;
   }


But How can I set isEnabled = false; for all TextBlocks in that listbox?

2 Answers 2

3

Don't do that. If there is virtualization the containers for some items will not even be there and you will need to deal with rather messy code to work around that issue. Try to bind IsEnabled instead and set the property/XML-attribute accordingly.

0
2

Simply iterate through all the items of list box using a foreach loop and do the same as you are already doing for one item

foreach (ListBoxItem item in yourListBox.Items)
        {
        // Getting the ContentPresenter of myListBoxItem
        ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(item );    
        // Finding textBlock from the DataTemplate that is set on that ContentPresenter
        DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
        TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
        // Do something to the DataTemplate-generated TextBlock
        myTextBlock.IsEnabled=false;
        }

BUT

this is not the recommended approach to do this. Instead you should use bindings for this purpose

Create a bool type property in your items source and bind the textbox IsEnabled property with it. When you want to disable/enable the textbox simply change that bool property and textbox will be automatically enabled or disabled based on bool value

 <TextBlock Name="textBlock" IsEnabled="{Binding path=SomeBoolProperty"} FontSize="14" Foreground="Blue">

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.