如何在ItemTemplate中将ItemsSource分配给ComboBox?

时间:2014-08-12 08:32:58

标签: c# wpf combobox listbox datatemplate

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

<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;

我希望在这个场景中也能实现类似上面的代码。这里的困难是我无法获得Column的名称,也不能使用.Columns,因为它们是DataTemplate。

注意:我没有使用MVVM。

1 个答案:

答案 0 :(得分:1)

试试这个

  

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));
    }
}
  

ViewModel

    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; }
}