簡體   English   中英

如何在ItemTemplate中將ItemsSource分配給ComboBox?

[英]How to assign ItemsSource to ComboBox in ItemTemplate?

這是我當前的代碼,簡化為相關代碼:

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

嘗試這個

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

暫無
暫無

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

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