首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF -强制绑定不可见的ComboBox

WPF -强制绑定不可见的ComboBox
EN

Stack Overflow用户
提问于 2019-03-27 02:08:14
回答 1查看 173关注 0票数 1

我有一个包含多个用户控件的WPF窗口,其中一些控件是不可见的(Visibility = Hidden)。其中一个控件有一个具有ItemsSource绑定的ComboBox,我希望在加载窗口/控件时预先设置它的选定项。

但是,直到组合框可见时,绑定才会被应用。当我要设置SelectedItem属性时,在调试器中遇到断点时,我注意到此时ItemsSource为null。有没有一种方法可以强制WPF应用数据绑定并填充组合框,同时使其不可见?

可重现的例子:

MainWindow.xaml

代码语言:javascript
复制
<Window x:Class="HiddenComboBoxBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HiddenComboBoxBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Border x:Name="comboboxParent" Visibility="Collapsed">
            <ComboBox x:Name="cmbSearchType" SelectedIndex="0" ItemsSource="{Binding SearchTypeOptions}" DisplayMemberPath="Name" SelectionChanged="cmbSearchType_SelectionChanged" />
        </Border>
    </Grid>
</Window>

MainWindow.xaml.cs

代码语言:javascript
复制
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace HiddenComboBoxBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ViewModel viewModel { get; set; } = new ViewModel();

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = viewModel;

            // Add some or all of our search types - in the real code, there's some business logic here
            foreach (var searchType in SearchType.AllSearchTypes)
            {
                viewModel.SearchTypeOptions.Add(searchType);
            }

            // Pre-select the last option, which should be "Bar"
            cmbSearchType.SelectedItem = SearchType.AllSearchTypes.Last();
        }

        private void cmbSearchType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}

ViewModel.cs

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HiddenComboBoxBinding
{
    public class ViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<SearchType> SearchTypeOptions { get; set; } = new ObservableCollection<SearchType>();

        #region INotifyPropertyChanged Members
        private void NotifyPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }

    public class SearchType
    {
        // Source list of Search Types
        private static List<SearchType> _AllSearchTypes;
        public static List<SearchType> AllSearchTypes
        {
            get
            {
                if(_AllSearchTypes == null)
                {
                    _AllSearchTypes = new List<SearchType>();
                    _AllSearchTypes.Add(new SearchType() { Name = "Foo" });
                    _AllSearchTypes.Add(new SearchType() { Name = "Bar" });
                }
                return _AllSearchTypes;
            }
        }

        // Instance properties - for the purposes of a minimal, complete, verifiable example, just one property
        public string Name { get; set; }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-03-27 04:03:48

我能够弄清楚问题所在。设置SelectedItem实际上是有效的(尽管当时ItemsSource为空),但是在XAML中,ComboBox的SelectedIndex=为“0”,并且它优先于在代码隐藏中设置的SelectedItem。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55363695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档