2015-12-13 32 views
0

我已经定义了这样一个ListView:UWP的ListView没有数据

<ListView x:Name="phonesListView" 
        Grid.Row="5" 
        Background="Black" 
        IsItemClickEnabled="True" 
        Foreground="Gray" > 

       <ListView.ItemContainerStyle> 
        <Style TargetType="ListViewItem"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="ListViewItem"> 
            <Grid> 
             <Border x:Name="myback" Background="Transparent"> 
              <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="20, 0, 20, 0"/> 
             </Border> 
            </Grid> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </ListView.ItemContainerStyle> 

       <ListView.ItemTemplate> 
        <DataTemplate> 
         <Grid Width="auto" HorizontalAlignment="Stretch"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="*"/> 
           <RowDefinition Height="*"/> 
           <RowDefinition Height="*"/> 
          </Grid.RowDefinitions> 

          <Grid 
          Grid.Row="0"> 
           <Grid.ColumnDefinitions > 
            <ColumnDefinition Width="3*" /> 
            <ColumnDefinition Width="*" /> 
           </Grid.ColumnDefinitions> 

           <ComboBox 
            Grid.Column="0" 
            VerticalAlignment="Center" 
            Background="Black" 
            Foreground="White" >   
            <ComboBoxItem Content="Home" IsSelected="True"/> 
            <ComboBoxItem Content="Work"/> 
            <ComboBoxItem Content="Office"/> 
            <ComboBoxItem Content="Fax"/> 
            <ComboBoxItem Content="School"/> 
           </ComboBox> 

           <Button 
            Grid.Column="1" 
            Height="30" 
            Width="30" 
            Foreground="Black" 
            Margin="0, 5, 0, 5" 
            HorizontalAlignment="Center" Click="RemovePhone"> 
            <Button.Background> 
             <ImageBrush Stretch="Fill" ImageSource="Assets/appbar.close.png" /> 
            </Button.Background> 
           </Button> 

          </Grid> 

          <TextBox 
           Grid.Row="1" 
           Background="White" 
           Foreground="Black" 
           FontSize="18" 
           Text="{Binding Number}" 
           InputScope="TelephoneNumber" /> 

         </Grid> 
        </DataTemplate> 
       </ListView.ItemTemplate> 

      </ListView> 

在代码中,我的属性:

ObservableCollection<Phone> phones = new ObservableCollection<Phone>(); 

一切工作正常,我可以添加并可以删除列表视图中的项目

phones.Add or phones.Remove 

问题是当我离开页面或保存按钮被按下这个集合的计数正好是它是但我没有填写输入字段中的数据。你能帮我解决吗?谢谢。

回答

0

如果您希望ListView中的编辑数据回流到项目以便保存编辑,您必须使用双向绑定。当控件(在TextBox以下的示例中)失去焦点时,Text属性的值将被存回到绑定字段中(在您的示例数字中)。

<TextBox 
    Grid.Row="1" 
    Background="White" 
    Foreground="Black" 
    FontSize="18" 
    Text="{Binding Number, Mode=TwoWay}" 
    InputScope="TelephoneNumber" /> 

有关装订的更多详情,请阅读MSDN article