c# - 在我的 C# XAML Windows 8.1 应用程序中,如何迭代我的 ListView?

标签 c# xaml listview data-binding windows-8.1

我是 Windows 8.1 开发、XAML 和 C# 的新手,所以如果这个问题很初级,请原谅我。

我有一个 <Page>在我的应用程序中包含 <ListView> ,像这样:

<ListView ItemsSource="{Binding Mode=TwoWay}" x:Name="ListView_Statistical">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Width="100"  Margin="10,20">
                    <Run Text="X/Y " />
                    <!--<Run Text="{Binding Source={StaticResource ThisPage}, Path=i}" />-->
                </TextBlock>
                <TextBox HorizontalAlignment="Left" Text="{Binding xVal}" PlaceholderText="X" InputScope="Number" FontSize="28" Width="100" Margin="0,10,10,10" />
                <TextBox HorizontalAlignment="Left" Text="{Binding yVal}" PlaceholderText="Y" InputScope="Number" FontSize="28" Width="100" Margin="0,10,10,10" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

在代码隐藏中,我像这样设置它的 DataContext:

ListView_Statistical.DataContext = this.statisticalPoints;

this.statisticalPoints定义如下:

public ObservableCollection<StatisticalPoint> statisticalPoints
{
    get { return (ObservableCollection<StatisticalPoint>)GetValue(statisticalPointsProperty); }
    set { 
        SetValue(statisticalPointsProperty, value);
        NotifyPropertyChanged("statisticalPoints");
    }
}

// Using a DependencyProperty as the backing store for statisticalPoints.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty statisticalPointsProperty =
    DependencyProperty.Register("statisticalPoints", typeof(ObservableCollection<StatisticalPoint>), typeof(EnterCalc), new PropertyMetadata(0));

我不确定是否有必要将其设为 DependencyProperty,或者是否有必要使其遵循 INotifyPropertyChanged,但它们似乎伤害

无论如何,所以在我的构造函数中,我向我的统计点添加了一堆东西:

this.statisticalPoints = new ObservableCollection<StatisticalPoint>();
this.statisticalPoints.Add(new StatisticalPoint() { xVal = 1.0, yVal = 2.0 });
this.statisticalPoints.Add(new StatisticalPoint() { xVal = 33.0, yVal = 44.0 });
this.statisticalPoints.Add(new StatisticalPoint() { xVal = 555.0, yVal = 666.0 });
this.statisticalPoints.Add(new StatisticalPoint() { xVal = 0.7, yVal = 0.8 });

当我加载页面时,我确实在 ListView 中看到了五行,按照我对 this.statisticalPoints 的初始化中的定义进行填充.

我遇到问题的部分是:

我更改了第一个 <TextBox> 中的第一个值在ListView ,然后点击我的保存按钮...但是 ListView.Items 没有反射(reflect)我的更改,我不知道如何查看 <TextBox>本身。

我真正想做的是让我的用户可以修改这一堆统计点并能够保存他们的更改。为此,我觉得我需要读取 <TextBox> 中的值es,但我不知道该怎么做。

或者,如果执行此操作的“正确方法”是将数据保存在 this.statisticalPoints 中在 <TextBox> 中进行更改时是最新的es,然后我认为一个TwoWay绑定(bind)模式可以做到,但 ListView.Items 都不行也不this.statisticalPoints当我在 <TextBox> 中进行更改时更改.

没有在那些 <TextBox> 中设置事件处理程序元素,如您所见,但我是否需要它们,还是我遗漏了一些明显的东西?

在此先感谢您能给我的任何帮助!

最佳答案

要解决您的初始 问题,请为每个文本框Mode="TwoWay" 进行绑定(bind)。由于我无法理解的原因,该模式在 Windows 应用商店应用程序中的几乎所有内容上都是默认的 OneWay

以两种方式绑定(bind) ItemsSource 几乎没有任何作用,因为 UI 不会更改集合本身(通过更改,我的意思是完全替换)。要遍历您的集合,只需遍历 this.statisticalPoints,它将包含当前数据。

现在,您还有很多其他的误解,所以要尝试并解决它们:

  • 您从未显示过保存按钮,但绑定(bind)要么更新您的源,要么不更新。保存按钮通常用于保存从 View 模型到模型的更改。
  • 说到 View 模型,您似乎没有。您不应该直接设置控件的数据上下文,当然也不应该在代码隐藏中有这么多。为您的页面创建合适的 View 模型对象,并将 ItemsSource 绑定(bind)到该 View 模型的公共(public)属性。
  • 集合上的 NotifyPropertyChanged 通常 是不必要的,除非您要在代码中替换集合。
  • 拥有它不会有什么坏处,除了 DependencyProperty (DP) 的支持属性的 setter 从不 被框架调用,所以把它放在那里只是奇怪
  • 而且你根本不需要摄影指导。 DP 在那里,因此父控件可以将数据绑定(bind)到您的特殊用户控件。在您使用用户控件并真正了解 DP 的工作原理之前,您不需要使用它们。

关于c# - 在我的 C# XAML Windows 8.1 应用程序中,如何迭代我的 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29639702/

相关文章:

c# - 防止 Entity Framework 将外键设置为 NULL

c# - 将文本写入文本框

c# - 将 ObservableCollection<string> 双向绑定(bind)到 WPF DataGrid

Android 如何识别来自外部适配器的 Listview 按钮单击事件?

c# - 找不到类型或命名空间名称 'NativeShare'(是否缺少 using 指令或程序集引用?)

c# - 访问 TreeView 中的父节点?

wpf - 如何使覆盖控件高于所有其他控件?

listview - 如何制作扩展卡?

java - Android 在使用 ListView 和 Bitmap 时出现内存不足错误

c# - 如何以编程方式在 Windows 服务应用程序中创建暂停?