【问题标题】:change textblock text that is inside Listbox in windowsphone 8更改 windowsphone 8 中列表框内的文本块文本
【发布时间】:2014-11-01 05:59:42
【问题描述】:

我想更改页面初始化事件中的文本块文本 这是我的 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>

我想在代码隐藏中使用 c# 更改 txtDate.Text,但是在后面的代码中无法访问 txtdate,那么如何实现呢?

【问题讨论】:

  • 你试过txtDate而不是txtdate吗? TextBlock 元素当然应该由自动生成的 C# 公开为具有您给它的名称的可访问成员。
  • @PeterDuniho 我犯了同样的错误 - txtDate 包含在他用于 &lt;ListBox.ItemTemplate&gt; 的 DataTemplate 中。
  • @furkle:有道理...感谢您指出这一点!

标签: c# xaml windows-phone-8 listbox


【解决方案1】:

您无法访问 txtDate 对象的原因是它包含在您用于 ListBox 的 DataTemplate 中。这不是错误 - DataTemplate 将应用于添加到您的 ListBox 的每个项目。

鉴于 ListBox 在其他控件中创建了一个包含名称为“txtDate”的 TextBlock 的 Grid,对于添加到其中的每个项目,访问 txtDate 对象意味着什么?当您引用 txtDate 时,您的程序将如何确定与相同数量的 ListBoxItem 关联的(功能上)无限数量的 txtDate 中的哪一个?

如果您希望能够轻松更改 txtDate 的内容,您需要将 ListBox 的 ItemsSource 绑定到 ViewModel 中的属性。最简单的方法是让该属性成为包含自定义模型类型的 IEnumerable。这样,您可以更新该模型的 text 属性并对该属性调用 NotifyPropertyChanged,然后 UI 将更新以反映新数据。

这是一个例子:

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
}

然后您希望将 ListBox 的 ItemsSource 绑定到 YourViewModel 的 Models 属性,并将 TextBox 的文本绑定到 YourModel 的 YourText 属性。每当您更改 YourModel.YourText 属性时,它都会在 UI 上自动更新。我认为让你的模型实现 INotifyPropertyChanged 是否是正确的 MVVM 可能会引起争论,但我发现在这些情况下,它比每次对其中一个模型进行更改时强制 ViewModel 更新每个模型要容易得多。

如果您不熟悉 WPF 使用的 MVVM 模式,这可能是一个好的开始:MVVM example

【讨论】:

  • 如果我将文本块放在列表框之外,而不是在代码行为中可访问,但当它在列表框内时,它无法访问 @furkle 即我无法实现此 txtDate.Text ="anything";
  • 那么有什么解决办法呢?我不能改变 txtDate.text?? @furkle
  • @SmartDeveloper 我添加了一个非常简短的代码示例 - 也许它会有所帮助。
【解决方案2】:

此功能将帮助您...这将帮助您在列表框运行时中找到控件..

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;
        }

这里的第一个参数是父级,第二个参数是元素的名称,在你的例子中是“txtDate”。希望它有效!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多