问题描述:
希望能按列表的形式显示多段文字,且支持双向绑定(数据更新了界面也会变,界面修改了内容数据也会更新)
代码:
前台
放一个文本框控件(用作对比),放一个列表控件,列表里面嵌入文本框(用数据模板 DataTemplate来做)。
定义一下数据模板资源
<Window.Resources>
给 ListView 的 ItemTemplate 用的数据模板。
<DataTemplate x:Key="LV_ItemTemplateString">
直接绑定 string 类型,用 '.' 来绑定
<TextBox Text="{Binding Path=. ,Mode=TwoWay}"></TextBox>
</DataTemplate>
<DataTemplate x:Key="LV_ItemTemplateClass">
每个Item里面都包含一个TextBox, TextBox 的 Text 内容绑定到某个对象的 Text 属性上。
这里所说的某个对象会在后台代码里给出来。
<TextBox Text="{Binding Path=Text ,Mode=TwoWay}"></TextBox>
</DataTemplate>
</Window.Resources>
界面
<Grid>
<StackPanel>
单个字符串,字符串数组,包装成类的字符串数组
<TextBox x:Name="TB" Height="25" Text="{Binding Path = SingleComment, Mode=TwoWay}"></TextBox>
<ListView x:Name="LV_StringArray" Height="100" ItemTemplate="{StaticResource LV_ItemTemplateString}"/>
<ListView <

文章介绍了如何在WPF应用中实现列表控件(ListView)对多段文字的双向绑定。关键在于使用ObservableCollection和数据模板(DataTemplate),以及实现INotifyPropertyChanged接口的Comment类,确保界面UI更新后能同步到数据模型中。
最低0.47元/天 解锁文章
4227

被折叠的 条评论
为什么被折叠?



