【问题标题】:Populate combobox with collection other than the one the Grid it is in is bound to用集合填充组合框,而不是它所在的 Grid 绑定到的集合
【发布时间】:2011-04-16 16:12:22
【问题描述】:

我有以下代码:

<Grid DataContext="{Binding ItemTypes}">
            ...

            <TextBlock Text="Name:" />
            <TextBox Grid.Column="2" Text="{Binding Name}" />

            <TextBlock Grid.Row="1" Text="Description:" />
            <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Description}" />

            <TextBlock Grid.Row="2" Text="Manufacturer:" />
            <ComboBox Grid.Column="1" Grid.Row="2" />

            <TextBlock Grid.Row="3" Text="Short Name:" />
            <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding ShortName}" />
        </Grid>

设置 Grid 的 DataContext 的 ItemTypes 来自包含另一个集合的 ViewModel。此网格内的制造商组合框需要填充其他集合。我试过这个:

     ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
 AncestorType=Window, AncestorLevel=1}, Path=DataContext.companies}"

但它没有用。如何让组合框填充除 Grid 绑定的集合之外的其他集合?

ViewModel 代码:

    public class ItemTypeViewModel
    {
        #region private fields
        private ICollectionView collectionView;
        private IItemTypeService itemTypeService;
        #endregion

        #region automatic properties
        public ObservableCollection<ItemTypeViewModel> ItemTypes { get; private set; }
        public IEnumerable<Company> companies { get; private set; }
        #endregion properties

        #region constructors
        public ItemTypeAdminViewModel(IItemTypeService itemTypeService)
        {
            this.itemTypeService = itemTypeService;
            Initialize();
            collectionView = CollectionViewSource.GetDefaultView(ItemTypes);
        }
        #endregion

        #region private methods
        private void Initialize()
        {
            //TODO figure out if I should I wrap in Try/Catch here
            ItemTypes = new ObservableCollection<ItemTypeViewModel>(itemTypeService.GetItemTypes());
            companies = itemTypeService.GetCompanies();
        }
        #endregion

        #region commands
        public ICommand GoToFirst
        {
            get
            {
                return new RelayCommand(() => collectionView.MoveCurrentToFirst(),
                    () => collectionView.CurrentPosition >= 1);
            }
        }

        public ICommand GoToLast
        {
            get
            {
                return new RelayCommand(() => collectionView.MoveCurrentToLast(),
                    () => collectionView.CurrentPosition < (ItemTypes.Count - 1));
            }
        }

        public ICommand NextCommand
        {
            get
            {
                return new RelayCommand(() => collectionView.MoveCurrentToNext(),
                    () => collectionView.CurrentPosition < (ItemTypes.Count - 1));
            }
        }

        public ICommand PreviousCommand
        {
            get
            {
                return new RelayCommand(() => collectionView.MoveCurrentToPrevious(),
                    () => collectionView.CurrentPosition >= 1);
            }
        }
        #endregion
    }
}

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    假设您的视图模型设置为Window.DataContext,而您的其他集合MyOtherCollection 是视图模型中的一个属性。将您的 ComboBox 更改为以下内容:

    <ComboBox Grid.Column="1" Grid.Row="2"
              ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=DataContext.MyOtherCollection}" />
    

    【讨论】:

    • 感谢您的回答,虽然我试过了,但它对我不起作用。仍然没有显示任何内容。
    【解决方案2】:

    在其构造函数中分配给视图的数据上下文的内容。如果通过构造函数注入将view的datacontext指定为viewmodel,应该可以直接绑定为ItemsSource="{Binding companies}"

    【讨论】:

    • 表单的 XAML 代码实际上是一个用户控件。它获取的数据(例如网格中的 ItemTypes)来自包含此用户控件的页面,并设置了其 DataContext。
    【解决方案3】:

    我想说只是不要在Grid 上设置DataContext。相反,单独绑定每个项目。除非您提供更多信息 - 特别是您的虚拟机,否则很难提供进一步的建议。

    【讨论】:

    • DataContext 设置为 ItemTypes(我发布的 XAML 代码)的 Grid 是一个用户控件。它的 ItemTypes 来自它包含的 Window 设置为的 ViewModel。你还认为设置每一项都是强制性的吗?
    猜你喜欢
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多