0

i want to change textblock text in page initialize event here is my xaml

  <ListBox   Margin="3,60,1,10" BorderThickness="2" Grid.Row="1" Name="lstAnnouncement" Tap="lstAnnouncement_Tap" Width="476" d:LayoutOverrides="VerticalMargin">
        <ListBox.ItemTemplate>

            <DataTemplate>

                <StackPanel   Name="thispanel"  Grid.Row="1" Orientation="Horizontal" Height="120" Width="478" >

                    <StackPanel.Background>
                        <ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />

                    </StackPanel.Background>

                    <Grid  HorizontalAlignment="Left" Width="30" Margin="0,0,0,2" Background="#FF0195D5" Height="118">

                        <TextBlock  x:Name="txtDate" TextWrapping="Wrap">

                        </TextBlock>
                    </Grid>
                  </StackPanel>

            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

i want to change txtDate.Text using c# in code-behind but txtdate is not accessible in code behind so how to achieve it ?

2
  • Did you try txtDate instead of txtdate? The TextBlock element certainly should be exposed by the auto-generated C# as an accessible member having the name you gave it. Nov 1, 2014 at 6:11
  • @PeterDuniho I made the same error - txtDate is contained within the DataTemplate he's using for <ListBox.ItemTemplate>.
    – furkle
    Nov 1, 2014 at 6:36

2 Answers 2

2

The reason you're not able to access the txtDate object is because it's contained within the DataTemplate you're using for the ListBox. This isn't an error - the DataTemplate is being applied to every single item added to your ListBox.

Given that the ListBox creates, among other controls, a Grid containing a TextBlock with the name "txtDate", for every single item added to it, what would it mean to access the txtDate object? How would your program decide which of a (functionally) infinite number of txtDates associated with an identical number of ListBoxItems you meant when you referenced txtDate?

If you wanted to be able to easily change the content of txtDate, you'd want to bind the ItemsSource of your ListBox to a property in a ViewModel. The easiest way to do this would be to have that property be an IEnumerable containing a custom model type. This way, you could update the text property of that model and call NotifyPropertyChanged on the that property, and the UI would update to reflect the new data.

Here's an example:

public class YourViewModel
{
    public List<YourModel> Models { get; set; }
}

public class YourModel : INotifyPropertyChanged
{
     private string yourText;
     public string YourText 
     { 
         get { return yourText; }
         set
         {
             yourText = value;
             NotifyPropertyChanged("YourText");
         }
      }

      // add INotifyPropertyChanged implementation here
}

And then you'd want to bind the ItemsSource of the ListBox to YourViewModel's Models property, and the text of your TextBox to the YourModel's YourText property. Any time you change the YourModel.YourText property, it'll automatically update on the UI. I think it's probably subject to debate whether having your model implement INotifyPropertyChanged is proper MVVM, but I find it a lot easier in these cases than forcing the ViewModel to update every single model each time a change is made on one of them.

If you're not familiar with the MVVM pattern used with WPF, this might be a good start: MVVM example.

3
  • if i put textblock outside listbox than it accessible in codebehing but when it inside listbox it is not accessible @furkle i.e i cant achieve this txtDate.Text ="anything";
    – SD7
    Nov 1, 2014 at 6:27
  • so what can be the solution? i cant change txtDate.text?? @furkle
    – SD7
    Nov 1, 2014 at 6:40
  • @SmartDeveloper I've added a really brief code sample - maybe it'll be helpful.
    – furkle
    Nov 1, 2014 at 6:57
1

this function will help you... This will help u find the control inside of a listbox runtime..

public FrameworkElement SearchVisualTree(DependencyObject targetElement, string elementName)
        {
            FrameworkElement res = null;
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return res;

            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if ((child as FrameworkElement).Name == elementName)
                {
                    res = child as FrameworkElement;
                    return res;
                }
                else
                {
                    res = SearchVisualTree(child, elementName);
                    if (res != null)
                        return res;
                }
            }
            return res;
        }

Here first parameter is parent and the second parameter is the name of the element which in your case is "txtDate".. hope it works!!

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.