【问题标题】:XAML - Bind combobox in DataTemplate to a collection?XAML - 将 DataTemplate 中的组合框绑定到集合?
【发布时间】:2013-09-26 13:40:30
【问题描述】:

首先,感谢您抽出时间阅读这篇文章。非常感谢所有贡献。

我很难理解如何将 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 中的 2 个 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);
    }

如何将集合 Data templatesFooterstemplatesHeader 放入各自 ComboBox 的 ItemsSources 中?

数据模板用于列表框。

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

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

【问题讨论】:

    标签: c# wpf xaml combobox datatemplate


    【解决方案1】:

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

    ItemsSource="{Binding TemplatesHeader, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
    
    public ObservableCollection<TemplateHeaderFooter> TemplatesHeader
    {
      get{return templatesHeader;}
    }
    

    你也可以为其他财产做同样的事情

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 1970-01-01
      • 2014-02-15
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多