2014-02-13 132 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