- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过将 String
绑定(bind)到 ListBox
的 ItemsSource
属性来操作 ObservableCollection
> 并将项目模板设置为 TextBox
。
我的问题是,当我在 ListBox
包含的 TextBox
项目中编辑它们时,ObservableCollection
中的项目没有得到更新。我做错了什么?
最小工作示例的 XAML 是
<Window x:Class="ListBoxUpdate.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBoxUpdate" Height="300" Width="300"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Show items" Click="HandleButtonClick"/>
<TextBlock Grid.Row="1" x:Name="textBlock" />
</Grid>
<ListBox
Grid.Column="1"
ItemsSource="{Binding Strings, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding ., Mode=TwoWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
而相应的代码隐藏是
using System;
using System.Collections.ObjectModel;
using System.Windows;
namespace ListBoxUpdate
{
public partial class Window1 : Window
{
public ObservableCollection<string> Strings { get; set; }
public Window1()
{
InitializeComponent();
Strings = new ObservableCollection<string>(new string[] { "one", "two", "three" });
this.DataContext = this;
}
void HandleButtonClick(object sender, RoutedEventArgs e)
{
string text = "";
for (int i = 0; i < Strings.Count; i++) {
text += Strings[i] + Environment.NewLine;
}
textBlock.Text = text;
}
}
}
非常感谢您的建议。
最佳答案
正如@BrandonKramer 和@Peter Duniho 的评论所示,解决方案是数据绑定(bind)不能更改它所绑定(bind)到的对象本身,只能更改该对象的属性。
因此,我必须创建一个包装器类,其属性将成为我要操作的字符串。代码隐藏现在是
using System;
using System.Collections.ObjectModel;
using System.Windows;
namespace ListBoxUpdate
{
public partial class Window1 : Window
{
public ObservableCollection<StringWrapper> Strings { get; set; }
public class StringWrapper
{
public string Content { get; set; }
public StringWrapper(string content)
{
this.Content = content;
}
public static implicit operator StringWrapper(string content)
{
return new Window1.StringWrapper(content);
}
}
public Window1()
{
InitializeComponent();
this.Strings = new ObservableCollection<StringWrapper>(new StringWrapper[] { "one", "two", "three" });
this.DataContext = this;
}
void HandleButtonClick(object sender, RoutedEventArgs e)
{
string text = "";
for (int i = 0; i < Strings.Count; i++) {
text += Strings[i].Content + Environment.NewLine;
}
textBlock.Text = text;
}
}
}
并且 XAML 只需修改一次
<ListBox
Grid.Column="1"
ItemsSource="{Binding Strings, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Content, Mode=TwoWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
谢谢@BrandonKramer 和@Peter Duniho。
更新:我觉得定义一个包装器只是为了操纵对象感到不舒服,而且在列表中编辑对象本身而不是它的一个属性的问题对我来说似乎是普遍的,所以我试图找到另一种解决方案。我宁愿 Hook ItemTemplate
中 TextBox
的 LostFocus
事件。
在这种情况下,问题是从刚刚失去焦点的模板化 TextBox
中找到 ListBox
中的索引。我无法使用 ListBox
的 SelectedIndex
属性,因为在 LostFocus
触发时,它已经设置为不同的 ListBoxItem
。
我进行了大量搜索,并在此处找到了 Dennis Troller 的答案:WPF ListBoxItems with DataTemplates - How do I reference the CLR Object bound to ListBoxItem from within the DataTemplate?诀窍是让 TextBox
的 DataContext
失去焦点,并使用 ListBox
的 ItemContainerGenerator
首先识别容器(使用 ItemContainerGenerator.ContainerFromItem
)然后获取列表中的索引(使用 ItemContainerGenerator.IndexFromContainer
)。
现在是代码隐藏
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
namespace ListBoxUpdate
{
public partial class Window1 : Window
{
public ObservableCollection<string> Strings { get; set; }
public Window1()
{
InitializeComponent();
this.Strings = new ObservableCollection<string>(new string[] { "one", "two", "three" });
this.DataContext = this;
}
void HandleButtonClick(object sender, RoutedEventArgs e)
{
string text = "";
for (int i = 0; i < Strings.Count; i++) {
text += Strings[i] + Environment.NewLine;
}
textBlock.Text = text;
}
void HandleTextBoxLostFocus(object sender, RoutedEventArgs e)
{
// https://stackoverflow.com/questions/765984/wpf-listboxitems-with-datatemplates-how-do-i-reference-the-clr-object-bound-to?rq=1, @Dennis Troller's answer.
int index;
object item;
DependencyObject container;
TextBox textBox = sender as TextBox;
if (textBox == null) return;
item = textBox.DataContext;
container = listBox.ItemContainerGenerator.ContainerFromItem(item);
if (container != null) {
index = listBox.ItemContainerGenerator.IndexFromContainer(container);
if (textBox.Text != Strings[index]) {
Strings[index] = textBox.Text;
}
}
}
}
}
完整的 XAML 如下(我单向绑定(bind)了文本):
<Window x:Class="ListBoxUpdate.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBoxUpdate" Height="300" Width="300"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Show items" Click="HandleButtonClick"/>
<TextBlock Grid.Row="1" x:Name="textBlock" />
</Grid>
<ListBox
x:Name="listBox"
Grid.Column="1"
ItemsSource="{Binding Strings}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox
Text="{Binding ., Mode=OneWay}"
LostFocus="HandleTextBoxLostFocus"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
关于c# - ObservableCollection<string> 中的项目在绑定(bind)到 WPF 列表框时不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42459063/
这是一个有趣的案例,我无法在网上找到任何信息。我正在尝试创建一个网格,需要将 ObservableCollection 的 ObservableCollection 绑定(bind)到它。想象这样一个
如何复制ObservableCollection项目到另一个 ObservableCollection没有引用第一个集合?这里ObservableCollection影响两个集合的项目值更改。 代码
public class Alpha { public ObservableCollection Items { get; set; } public Alpha() {
我只是想知道如何拥有父集合的子集合? 例如, 我已经有一个 ObservableCollection 产品,它正在添加并正确绑定(bind)到 XAML。但是,现在我需要另一个包含产品项目的 Obse
我对 Silverlight 体验相对较新,我正在尝试创建一个带有 DomainService 的 MVVM 应用程序,该应用程序将 POCO 作为模型返回。我有一个 UserControl,它有一个
查看 Microsoft 站点上的 Windows 运行时引用,我能找到的唯一相关集合是 IObservableVector 。 .NET Projection ObservableCollectio
我正在尝试获取值“thisValueIwant”。有没有可能如此容易地获得这个值(value)?或者也许这两个 ObservableCollection 有另一种解决方案 public class F
我有一个 ObserveableCollection,其中包含另一个 ObserveableCollection。在我的 WPF 中,我设置了对 Persons.Lectures 的绑定(bind)。
我有一个包含 20 个项目(图像)和按钮(“下一个”)的 observablecollection。我如何获得像 observablecollection.next() 和 observablecol
我有一个 ObservableCollection . T 有一个 ToString() 方法。我想做的是转换 ObservableCollection至 ObservableCollection .
我有一个 DataGrid,它绑定(bind)到 ViewModel 中的一个 ObservableCollection。这是一个搜索结果DataGrid。问题是,在我更新搜索结果 Observabl
有一堆ObservableCollection Result并要求将它们全部组合成另一个 ObservableCollection AllResults所以我可以在 listview 中显示它. 只是
哪个是保存我的数据的更好解决方案,还是取决于某些条件? 示例情况 1: 您需要显示一个数据列表,选择后可以在新窗口中修改。 示例情况 2: 您需要显示可以在此列表中修改的数据列表。 最佳答案 当您使用
从 xml 中执行 ViewModel 的最佳方法是什么:
所以我有一个 BaseClass 以及继承自基类的几个子类 ChildClass1 ChildClass2 我有ObservableCollections需要就地排序的子类,我无法创建新的 Obser
我为 ObservableCollection 构建了一个简单的扩展方法 AddRange: using System; using System.Collections.Generic; using
我的情况是,我有“tablegenerateModel”类的 ObservableCollection,该类进一步包含“column_Data”类的 ObservableCollection,并且这个
我将有一个 7 个小“购物 list ”,然后是一个包含 7 个小 list 中所有项目的大 list 。 是否可以使用 databind 和 observablecollection,以便从小列表中
当所述 ObservableCollection 从 View 模型公开时,我无法找到正确的绑定(bind)语法来绑定(bind) ObservableCollection 中包含的项目的属性。 当我
我有以下类(class),效果很好 public class RemoteSource { ObservableCollection remote; string[] _servers
我是一名优秀的程序员,十分优秀!