【发布时间】:2015-04-15 00:36:29
【问题描述】:
我是 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 的初始化中的定义填充。
我遇到问题的部分是:
我在ListView 中更改第一个<TextBox> 中的第一个值,然后点击我的保存按钮...但是 ListView.Items 没有反映我的更改,我无法弄清楚如何查看在<TextBox> 本身。
我真正想做的是让我的用户可以修改这些统计点,并能够保存他们的更改。为此,我觉得我需要阅读<TextBox>es 中的值,但我不知道该怎么做。
或者,如果这样做的“正确方法”是在<TextBox>es 中进行更改时保持this.statisticalPoints 中的数据是最新的,那么我认为TwoWay 绑定模式会这样做,但是当我在 <TextBox> 中进行更改时,ListView.Items 和 this.statisticalPoints 都不会更改。
我没有在那些<TextBox> 元素中设置了事件处理程序,如您所见,但我需要它们,还是我遗漏了一些明显的东西?
提前感谢您能给我的任何帮助!
【问题讨论】:
标签: c# xaml listview data-binding windows-8.1