XAML - 将DataTemplate中的组合框绑定到集合中?

时间:2013-09-26 13:40:30

标签: c# wpf xaml combobox 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>

当我的Window加载时,我从我的数据库下载集合数据并存储到本地集合中。请注意,有两个集合,一个用于我的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 &amp; templatesHeader 进入各自ComboBox的ItemsSources?

datatemplate用于ListBox。

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

非常感谢。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

在集合的变量上定义属性包装器,然后可以将它们绑定到组合框:

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

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

同样,你可以为其他财产做