简体   繁体   English

更改Windows Phone 8列表框中的textblock文本

[英]change textblock text that is inside Listbox in windowsphone 8

i want to change textblock text in page initialize event here is my xaml 我想在页面初始化事件中更改textblock文本,这是我的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 ? 我想在代码隐藏中使用c#更改txtDate.Text,但是在后面的代码中无法访问txtdate,那么如何实现呢?

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. 您无法访问txtDate对象的原因是,它包含在用于ListBox的DataTemplate中。 This isn't an error - the DataTemplate is being applied to every single item added to your ListBox. 这不是错误-DataTemplate应用于添加到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? 鉴于该列表框其他控件中创建,包含名为“txtDate”一个TextBlock一个网格,用于添加到它的每一个项目,这一切意味着什么访问 txtDate对象? 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? 当您引用txtDate时,您的程序将如何确定与功能相同的ListBoxItems相关联的(功能上)无限数量的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. 如果希望能够轻松更改txtDate的内容,则希望将ListBox的ItemsSource绑定到ViewModel中的属性。 The easiest way to do this would be to have that property be an IEnumerable containing a custom model type. 最简单的方法是使该属性为包含自定义模型类型的IEnumerable。 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. 这样,您可以更新该模型的text属性,并在该属性上调用NotifyPropertyChanged,UI将会更新以反映新数据。

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. 然后,您想将ListBox的ItemsSource绑定到YourViewModel的Models属性,并将TextBox的文本绑定到YourModel的YourText属性。 Any time you change the YourModel.YourText property, it'll automatically update on the UI. 每当您更改YourModel.YourText属性时,它都会在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. 我认为,让模型实现INotifyPropertyChanged是否是正确的MVVM可能要争论不休,但是在这些情况下,我发现比强制每次对其中一个模型进行更改来强制ViewModel更新每个模型要容易得多。

If you're not familiar with the MVVM pattern used with WPF, this might be a good start: MVVM example . 如果您不熟悉WPF所使用的MVVM模式,那么这可能是一个不错的开始: MVVM example

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!! 这里的第一个参数是parent,第二个参数是元素的名称,在您的情况下是“ txtDate”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM