【发布时间】:2016-06-15 06:04:08
【问题描述】:
我目前有一个正在解析 YAML 字典的应用程序。我有一个名为 Line 的模型,看起来像这样 -
public class Line
{
private ObservableDictionary<string, string> language = new ObservableDictionary<string, string>();
public Line()
{
Language = new Dictionary<string, string>();
}
// used with the YAML parser
[YamlMember(Alias = "language")]
public Dictionary<string, string> Language { get; set; }
}
您可能已经注意到我使用的是ObservableDictionary,它不是标准类型。我从这个other StackOverflow answer 中获取了代码。据我了解,它只是设置了必要的INotifyPropertyChanged 接口。
无论如何,对于我的Line,我有一个ListView,其中包含以语言缩写和文本框表示的翻译词典。为了更好地举例说明我在说什么,这里有一张图。
在我的 App.xaml 中,我为我的 ListView 定义了一个 DataTemplate - 它们看起来像这样:
<ListView
ItemTemplate="{StaticResource LinesTemplateItem}"
ItemsSource="{Binding Value.Language}"
SelectionMode="None">
</ListView>
...
<DataTemplate x:Key="LinesTemplateItem">
<StackPanel Background="Transparent">
<TextBlock Text="{Binding Key}" />
<TextBox Text="{Binding Value, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
一切似乎都应该可以正常工作。我的数据显示正确。但是,当我更改一个值时,它不会使用错误更新底层源:
Error: Cannot save value from target back to source.
BindingExpression: Path='Value' DataItem='Windows.Foundation.Collections.IKeyValuePair`2<String,String>';
target element is 'Windows.UI.Xaml.Controls.TextBox' (Name='null');
target property is 'Text' (type 'String').
从错误中,我猜出于某种原因,数据绑定针对的是整个 UI 控件而不是其中的文本。我一直在寻找一段时间,但我似乎无法弄清楚如何解决这个错误。任何帮助表示赞赏。谢谢。
【问题讨论】:
标签: c# xaml data-binding uwp