如何在WPF中绑定ListView中的ContentTemplate
我已经稍微简化了这个问题,但是我需要更改ListView项的样式和ControlTemplate,同时仍然绑定到ListView项源 以下是我的ListView定义:如何在WPF中绑定ListView中的ContentTemplate,wpf,binding,win-universal-app,windows-10-universal,contenttemplate,Wpf,Binding,Win Universal App,Windows 10 Universal,Contenttemplate,我已经稍微简化了这个问题,但是我需要更改ListView项的样式和ControlTemplate,同时仍然绑定到ListView项源 以下是我的ListView定义: <ListView x:Name="MyListView" ItemsSource="{Binding ListOfStrings}" ItemContainerStyle="{StaticResource MyListViewItemStyle}"/> 以及ItemCon
<ListView x:Name="MyListView"
ItemsSource="{Binding ListOfStrings}"
ItemContainerStyle="{StaticResource MyListViewItemStyle}"/>
以及ItemContainerStyle:
<Style x:Key="MyListViewItemStyle" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<DataTemplate>
<TextBlock x:Name="txtValue" Text="{Binding Mode=TwoWay}" />
</DataTemplate>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ListView的ItemSource类型为:List
我需要TextBlock(txtValue)通过绑定到ItemSource来显示一个项
ListView包含正确数量的项,但是如何绑定TextBlock
我需要在WPF通用Windows平台应用程序中执行此操作。我
在正常的WPF windows应用程序中测试了相同的代码,并且
工作正常。但在UWP应用程序中,ContentTemplate会
没有正确绑定
我肯定我错过了一些简单的东西。试试这个:
<Style x:Key="MyListViewItemStyle" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<TextBlock x:Name="txtValue" Text="{Binding }" />
</ControlTemplate>
</Setter.Value>
</Setter>
双向绑定需要路径或XPath
模板内容节的根目录不能包含“System.Windows.DataTemplate”类型的元素。只有FrameworkElement和FrameworkContentElement类型有效。找到了解决方案。仍在试图弄清楚它为什么有效,但它确实有效。您是否在单词Binding后遗漏了一个逗号?另外,我认为您可能会错过TextBox的模式,因为它默认为TwoWay,并且没有效果。我在ListView中仍然有正确数量的项(因此它正在拾取绑定),但是txtValue仍然不显示任何值。请尝试检查绑定和样式,这个示例对我来说很好。也许您需要更改textblock的前景。我应该提到,这是为使用VS2015的WPF通用Windows平台(UWP)应用程序完成的。我在一个普通的WPF应用程序中测试了相同的代码,一切都正常工作。这可能就是你让它工作的原因。我将相应地更新我的问题。