我有一个ItemsControl
有其ItemsSource
与一组Binding
给ObservableCollection
我的ViewModel
。我在其中使用Button
控件ItemTemplate
来显示我的数据,并且在中使用DataTemplate
,Button
并且没有任何Binding
数据被显示。以下是有问题的XAML:
<ItemsControl ItemsSource="{Binding Path=ConnectedInstruments}" HorizontalAlignment="Left">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Height="60" VerticalAlignment="Top">
<Button.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=ModelCode}" />
<TextBlock Text="{Binding Path=SerialNumber}" />
</StackPanel>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
TextBlock
内的两个控件均未Button
DataTemplate
填充。我知道这与Binding
路径有关,但是我对自己做错的事情感到茫然。有人可以让我走上正确的路吗?
编辑
public class Instrument
{
public string ModelCode { get; set; }
public string SerialNumber { get; set; }
}
public class ViewModel
{
public ObservableCollection<Instrument> ConnectedInstruments { get; set; }
}
我知道,ItemsControl
是Binding
正确与ObservableCollection
作为正确的计数Button
被显示的控制,只有模板数据丢失。
您需要使用任何特定原因ContentTemplate
而不是Content
像这样直接设置Button的原因吗?:
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Height="60" VerticalAlignment="Top">
<StackPanel>
<TextBlock Text="{Binding Path=ModelCode}" />
<TextBlock Text="{Binding Path=SerialNumber}" />
</StackPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句