如何将ItemsSource分配给ItemTemplate中的ComboBox

本文关键字:ItemTemplate 中的 ComboBox 分配 ItemsSource | 更新日期: 2023-09-27 18:17:30

以下是我当前的代码,简化为相关的:

<telerik:RadListBox Grid.Row="0" Grid.Column="2" Margin="0, 5, 5, 5"
            x:Name="listBoxIssue" HorizontalAlignment="Left" VerticalAlignment="Top"
            Height="690" Width="793"
            ItemTemplate="{StaticResource lbIssueTemplate}" ItemsSource="{Binding}">

模板:

<DataTemplate x:Key="lbIssueTemplate">
        <Grid Margin="0" Width="Auto">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>             
            <ComboBox x:Name="PrintCode" Grid.Row="0" Grid.Column="2" ItemsSource="{Binding}"
                      SelectedValuePath="PrintCode" DisplayMemberPath="PrintCode" />
        </Grid>
    </DataTemplate>

因此,有了这个绑定设置,应该有一个值附加到ComboBox的SelectedPath。现在我只需要将一个选择列表绑定到ComboBox。我用下面的代码在GridView中完成了这一点:

((GridViewComboBoxColumn)gridPrintOption.Columns["PrintCode"]).ItemsSource = listPrintCode;

我希望在这种情况下也能实现上面的代码。这里的困难是,我不能得到列的名称,也不能使用。columns,因为这些是DataTemplate。

注意:我不使用MVVM

如何将ItemsSource分配给ItemTemplate中的ComboBox

try this

xaml

    <Window.Resources>
    <DataTemplate x:Key="lbIssueTemplate">
        <Grid Margin="0" Width="Auto">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <ComboBox x:Name="PrintCode" Grid.Row="0" Grid.Column="2"
                      SelectedValue="{Binding DataContext.PrintCode, RelativeSource={RelativeSource Mode=Self}, Mode=TwoWay}" 
                      ItemsSource="{Binding DataContext.Choices, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
        </Grid>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ListBox Background="Red" ItemTemplate="{StaticResource lbIssueTemplate}" ItemsSource="{Binding Equips}"></ListBox>
</StackPanel>

xaml.cs

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

装备类,我只使用PrintCode属性

    public class Equip:INotifyPropertyChanged
{
   public Equip()
    {
        PrintCode = "All";
    }
    private string printcode;
    public string PrintCode
    {
        get { return printcode; }
        set { printcode = value; OnPropertychanged("PrintCode"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertychanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

视图模型

    public class ViewModel
{
    public ViewModel()
    {
        Choices = new ObservableCollection<string> { "All", "Left", "Right", "None" };
        Equips = new ObservableCollection<Equip>() { new Equip(), new Equip() };
    }
    public ObservableCollection<Equip> Equips { get; set; }
    public ObservableCollection<string> Choices { get; set; }
}