0

我有一张包含食物类型的桌子。每种食物类型每人 12 行。

我需要做的是在选择一个人之后,itemscontrol 将显示每种食物类型的 12 行。

到目前为止我已经成功了,但我想要的是每 12 行上方的标题说明食物类型,而不是在每一行上重复。

有谁知道实现这一目标的方法?

到目前为止,我唯一的方法是将 headereditemscontrol 放在 itemscontrol 中。对于这样一个简单的问题,这似乎是一种非常复杂的方法。

在此先感谢您的帮助。

4

1 回答 1

0

尝试:

xml:

   <StackPanel Grid.Row="1">
            <TextBlock Text="{Binding Path=FoodTypes[0].Description}" />
            <ItemsControl ItemsSource="{Binding Path=FoodTypes}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="15,0,0,0" Text="{Binding Path=Text}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>

代码:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MyViewModel();
        }
    }


    public class FoodType
    {
        public string Description {get;set;}
        public string Text {get;set;}
    }

    class MyViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<FoodType> foodTypes;

        public ObservableCollection<FoodType> FoodTypes
        {
            get { return this.foodTypes; }
            set
            {
                this.foodTypes = value;
                this.OnPropertyChanged("FoodTypes");
            }
        }

        public MyViewModel()
        {
            this.FoodTypes = new ObservableCollection<FoodType>();

            this.FoodTypes.Add(new FoodType() { Description = "Test1", Text = "Something" });
            this.FoodTypes.Add(new FoodType() { Description = "Test1", Text = "Something Else" });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if(handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
于 2015-06-19T20:37:11.680 回答