簡體   English   中英

XAML-將DataTemplate中的組合框綁定到集合嗎?

[英]XAML - Bind combobox in DataTemplate to a collection?

首先,感謝您抽出寶貴時間閱讀這篇文章。 非常感謝所有貢獻。

我很難理解如何將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);
    }

如何將集合數據templateFooterstemplatesHeader放入其各自ComboBox的ItemsSources中?

數據模板用於ListBox。

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

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

在集合變量上定義屬性包裝器,然后可以將它們綁定到組合框,如下所示:

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

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

同樣,您可以為其他財產做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM