我正在绑定(bind) List<string>
到我的ListBox
在 WPF 中使用 MVVM
目前我有
<ListBox ItemsSource="{Binding FileContents}"></ListBox>
我的 ViewModel 中的文件内容很简单
public List<string> FileContents {get;set;}
并且 FileContents 值在 ViewModel 的构造函数中设置,因此无需担心 INotifyProperty
目前一切正常。我可以在我的 ListBox
中看到列表随心所欲。
现在我需要提供一个模板!这是错误的地方
<ListBox ItemsSource="{Binding FileContents}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这就是问题所在!我的理解是我只需要做 <TextBox Text = "{Binding}"
因为 ListBox 已经绑定(bind)到 List<string>
属性(称为 FileContents)
但是,当我运行上面的 Visual Studio 时
The application is in break mode
如果我将代码更新为
<TextBox Text = "Some String Value"
然后它工作正常
我不明白我做错了什么。
最佳答案
设置Mode
的 Binding
至 OneWay
:
<TextBox Text="{Binding Path=., Mode=OneWay}" />
Text
的默认绑定(bind)模式TextBox
的属性是TwoWay
但是当你绑定(bind)到 string
时这将不起作用在List<string>
.
关于c# - 如何将字符串属性绑定(bind)到 ListBox 中的 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48283255/