【问题标题】:UWP ListView not dataUWP ListView 不是数据
【发布时间】:2016-03-18 22:04:13
【问题描述】:

我有一个这样定义的 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

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

【问题讨论】:

    标签: xaml uwp windows-10-mobile


    【解决方案1】:

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

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

    有关绑定的更多详细信息,请阅读MSDN article

    【讨论】:

      猜你喜欢
      • 2016-11-18
      • 2016-12-01
      • 2016-11-12
      • 2017-01-24
      • 1970-01-01
      • 2018-09-02
      • 2018-06-01
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多