2012-10-09 78 views
1

我已經在Google上進行了大量搜索,並且正在努力尋找一個簡單的示例來跟蹤ItemsControl,我認爲我需要執行以下操作。XAML佈局ItemsControl

我目前有包含複選框爲使用以下XAML佈局

<Grid> 
    <ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" 
ItemsSource="{Binding Selections}" Margin="12,22,12,94"> 
    <ListBox.ItemTemplate> 
      <DataTemplate> 
       <CheckBox IsChecked="{Binding IsChecked}" 
     Content="{Binding Path=Item.SelectionName}"> 
       </CheckBox> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 
預計該工程可滾動列表框網格

我現在想要一個文本框添加到每個複選框的右側,這樣它與1水平對齊複選框和每行1個文本框。我想我將不得不使用一個ItemsControl,讓兩個控件,但使用的ItemsControl下面我失去滾動

<Grid> 
    <ItemsControl ScrollViewer.VerticalScrollBarVisibility="Auto" 
    ItemsSource="{Binding Selections}" Margin="12,22,12,94"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <CheckBox IsChecked="{Binding IsChecked}" 
     Content="{Binding Path=Item.SelectionName}"> 
       </CheckBox> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 

同樣的能力,如果我嘗試和類似的文本框添加到

<DataTemplate> 
     <CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" /> 
     <TextBox /> 
    </DataTemplate> 

我得到一個錯誤The object 'DataTemplate' already has a child and cannot add 'TextBox'

從本質上講,我希望它看起來像這樣

enter image description here

任何人都可以給我一些關於如何構建XAML中的控件來獲得我想要的佈局的指針嗎?

回答

9

只需在DataTemplate中使用StackPanel並將方向設置爲水平。

<DataTemplate> 
    <StackPanel Orientation="Horizontal"> 
    <CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" /> 
    <TextBox /> 
    </StackPanel> 
</DataTemplate> 
+0

感謝這樣的工作,但不完全是這樣。它將TextBox放在複選框上,但複選框延伸過TextBox。有沒有辦法將一個Stackpanel分割成一列 - 我將編輯我的Q來顯示這個圖像。 – user3357963

+0

我當時有點朦朧,實際上這個效果很好。非常感謝你! – user3357963