UWP 双向绑定,在ListView中有个TextBox,怎么获取Text的值

时间:2023-11-11 21:19:14

要求:评论宝贝的时候一个订单里面包含多个产品,获取对产品的评论内容哦

1. xaml界面

  <ListView x:Name="lvDetail">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<RelativePanel Padding="10,0,0,0">
<Image x:Name="imgoods" Height="" Width="" Source="{Binding GoodsPicture}" />
<TextBlock x:Name="tbName" Text="{Binding GoodsName}" FontSize="" RelativePanel.RightOf="imgoods" Margin="10,0,0,0" />
<TextBlock x:Name="tbColor" Text="{Binding SpecValue}" Foreground="#C5C5C5" RelativePanel.Below="tbName" RelativePanel.RightOf="imgoods" Margin="8,5,0,0"/>
<TextBlock x:Name="tbUnitPrice" RelativePanel.AlignRightWithPanel="True" Margin="0,0,10,0">
<Run Text="¥" Foreground="Red"/>
<Run Text="{Binding UnitPrice}" FontSize="" Foreground="Red"/>
</TextBlock>
<TextBlock RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True" Margin="0,0,10,0">
<Run Text="x"/>
<Run Text="{Binding Quantity}" />
</TextBlock>
</RelativePanel>
<!-- 评价内容-->
<Grid Margin="" BorderBrush="Gray" BorderThickness="" CornerRadius="">
<TextBox x:Name="tbContent" Text="{Binding Path=Content, Mode=TwoWay}" BorderBrush="Transparent" TextWrapping="Wrap" Height="" PlaceholderText="亲,写点什么吧,您的意见对其他买家提供很多帮助"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

UWP 双向绑定,在ListView中有个TextBox,怎么获取Text的值

要求:获取ListView中x:Name为tbContent的值(评论内容)

第一步:绑定TextBox的值使用Mode=TwoWay

<TextBox x:Name="tbContent" Text="{Binding Path=Content, Mode=TwoWay}" BorderBrush="Transparent" TextWrapping="Wrap" Height="100" PlaceholderText="亲,写点什么吧,您的意见对其他买家提供很多帮助"/>

第二步:GoodsList集合的实体(也就是评论内容所在的实体模型)要实现INotifyPropertyChanged接口

   public class Goods : INotifyPropertyChanged
{
private string content;
/// <summary>
/// 内容(评价宝贝使用)
/// </summary>
public string Content
{
get
{
return content;
}
set
{
content = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Content"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
第三步:绑定数据源       
private ObservableCollection<Goods> GoodsList = new ObservableCollection<Goods>();//商品列表
lvDetail.ItemsSource = respOrder.OrderDetail.GoodsList;
GoodsList = respOrder.OrderDetail.GoodsList;
第四步:点击提交获取值
       在界面写评论内容会自动在数据源绑定的GoodsList中的Content属性中