2014-02-13 128 views
1

我有一个ItemsControl有其ItemsSource组与Binding在我ViewModelObservableCollection。我在其ItemTemplate中使用Button控件来显示我的数据,我在Button内使用DataTemplate,并且没有显示任何Binding数据。下面是有问题的XAML:正在填充ButtonDataTemplateXAML嵌套的DataTemplate数据绑定

<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控制。我知道这与Binding路径有关,但是我对于我做错了什么感到不知所措。有人能让我走上正轨吗?

编辑

public class Instrument 
{ 
    public string ModelCode { get; set; } 
    public string SerialNumber { get; set; } 
} 

public class ViewModel 
{ 
    public ObservableCollection<Instrument> ConnectedInstruments { get; set; } 
} 

我知道ItemsControlBinding正确与ObservableCollection为正在显示的Button控制正确的计数,仅模板数据丢失。

+0

你还可以显示你的数据结构吗?视图模型 –

+0

@VidasVasiliauskas - 我已经添加了代码 – Unflux

+0

您是否在绑定应用后的运行时间中设置了这些值?您也不需要路径属性只需写入Text =“{Binding ModelCode}” –

回答

1

您需要使用ContentTemplate而不是直接设置Button的Content这样的任何特定原因? :

<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <Button Height="60" VerticalAlignment="Top"> 
      <StackPanel> 
       <TextBlock Text="{Binding Path=ModelCode}" /> 
       <TextBlock Text="{Binding Path=SerialNumber}" /> 
      </StackPanel> 
     </Button> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
+0

可能是因为我是个笨蛋?那是行得通的。 – Unflux

+0

我不明白,我有限的英语能力是怀疑:)无论如何工作 – har07

+0

这是一种称自己为白痴的礼貌方式。谢谢。 [: – Unflux