2013-09-26 67 views
0

首先,感謝您抽出時間閱讀本文。所有的貢獻都非常感謝。XAML - 將DataTemplate中的組合框綁定到集合?

我很難理解如何將DataTemplate中的ComboBox ItemsSource綁定到ObservableCollection。

這裏是我的代碼迄今:

的DataTemplate模板(在模板底部注意到組合的):

<DataTemplate x:Key="ListBoxCustomTemplate"> 

      <Grid Margin="4" HorizontalAlignment="Stretch" x:Name="lstBoxItemRoomGrid" > 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 

      <Grid.RowDefinitions> 
       <RowDefinition /> 
       <RowDefinition /> 
       <RowDefinition /> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 

      <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" FontWeight="Bold" Text="{Binding TemplateGroupName}" /> 

      <Image x:Name="imgDeleteListBoxItem" Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Source="/Icons/Print-Groups-Config/delete-32.png" Height="25" Cursor="Hand" 
        ToolTip="Remove template" VerticalAlignment="Center" 
        HorizontalAlignment="Right" MouseLeftButtonUp="imgDeleteListBoxItem_MouseLeftButtonUp"> 
       <Image.Style> 
        <Style> 
         <Setter Property="Image.Visibility" Value="Hidden" /> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding IsMouseOver, ElementName=lstBoxItemRoomGrid}" Value="True"> 
           <Setter Property="Image.Visibility" Value="Visible" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Image.Style> 
      </Image> 

      <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding TemplateDescription}" TextWrapping="WrapWithOverflow" /> 

      <!-- Header Template Selection --> 
      <Label Grid.Row="2" Grid.Column="0" Margin="0,3,0,0" HorizontalAlignment="Left" VerticalAlignment="Bottom" Content="Select Header:" FontWeight="DemiBold" Foreground="DarkGray" /> 
      <telerik:RadComboBox x:Name="radComboHeaderTemplate" Grid.Row="3" Grid.Column="0" Width="120" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" 
           ClearSelectionButtonVisibility="Visible" SelectedValue="{Binding TemplateHeaderID}" /> 

      <!-- Footer Template Selection --> 
      <Label Grid.Row="2" Grid.Column="1" Margin="0,3,0,0" HorizontalAlignment="Left" VerticalAlignment="Bottom" Content="Select Footer:" FontWeight="DemiBold" Foreground="DarkGray" /> 
      <telerik:RadComboBox x:Name="radComboFooterTemplate" Grid.Row="3" Grid.Column="1" Width="120" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" 
           ClearSelectionButtonVisibility="Visible" SelectedValue="{Binding TemplateFooterID}" /> 

     </Grid> 
    </DataTemplate> 

當我的窗口負載,我從我的數據庫下載收集數據並存儲到本地收藏中。請注意,有兩個集合,一個用於DataTemplate中的兩個ComboBox。

//Header Templates 
    private ObservableCollection<TemplateHeaderFooter> templatesHeader = new ObservableCollection<TemplateHeaderFooter>(); 

    //Footer Templates 
    private ObservableCollection<TemplateHeaderFooter> templatesFooters = new ObservableCollection<TemplateHeaderFooter>(); 



    //--- Constructors --- 

    public PrintTemplateGroupsConfigWindow() 
    { 
     InitializeComponent(); 

     //Download Data From DB 
     this.templatesHeader = TemplateHeaderFootersDB.GetAllTemplatesOfType(1); 
     this.templatesFooters = TemplateHeaderFootersDB.GetAllTemplatesOfType(2); 
    } 

我如何收集數據templatesFooters & templatesHeader納入各自的組合框的的ItemsSources?

該數據模板用於ListBox。

<telerik:RadListBox x:Name="lstBoxPrintGroupTemplates" Height="300" Width="280" ItemsSource="{Binding}" IsEnabled="False" 
          ItemTemplate="{StaticResource ListBoxCustomTemplate}" Style="{StaticResource DraggableListBox}" > 

非常感謝。任何幫助表示讚賞。

回答

1

定義在你的收藏中的變量特性的包裝,然後你可以將它們綁定到組合框爲:

ItemsSource="{Binding TemplatesHeader, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 

public ObservableCollection<TemplateHeaderFooter> TemplatesHeader 
{ 
    get{return templatesHeader;} 
} 

同樣可以爲其他財產

+0

非常感謝你做的。您的解決方案完美運作 – RobHurd