【问题标题】:Windows 8 Combobox PlaceholderTextWindows 8 组合框占位符文本
【发布时间】:2013-06-27 11:55:41
【问题描述】:

我正在使用“visual studio express for windows 8”并使用组合框控件

<ComboBox Name="Categories" >
                <x:String>apple</x:String>
                <x:String>ball</x:String>
                <x:String>cat</x:String>
                <x:String>dog</x:String>                    
            </ComboBox>

我想在其中显示占位符文本以显示一些文本,直到用户没有从中选择任何项目。但是当我使用microsoft reference 中描述的属性 PlaceholderText 来显示文本但是当我使用它时,sdk 会显示此错误

成员“PlaceholderText”无法识别或无法访问。

或者是否有任何其他方法可以在 Combobox 中显示一些默认文本。 谢谢。

【问题讨论】:

    标签: windows-8


    【解决方案1】:

    这是针对 Windows 8.1 预览版而非 Windows 8 开发的。此时您需要安装预览版,然后才能使用此组合框进行开发。查看占位符的文档:

    Minimum supported client    Windows 8.1 Preview 
    

    编辑

    要手动执行此操作,只需手动预加载组合框。这是一个示例,让我们从 ViewModel 开始,其中构造函数会将初始值加载到名为“Loading”的组合框中

    public class MainVM : INotifyPropertyChanged
    {
    
        private List<string> _dataList;
    
        public List<string> ComboData
        {
            get { return _dataList; }
            set
            {
                if (_dataList != value)
                {
                    _dataList = value;
                    OnPropertyChanged();
                }
            }
        }
    
        public MainVM()
        {
            ComboData = new List<string> {"Loading..."};
        }
    
        #region INotify Property Changed Implementation
        /// <summary>
        /// Event raised when a property changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    
        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The name of the property that has changed.</param>
        public void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
    

    现在在主页面上,xaml 绑定到 ComboData,但我们需要警惕第一种情况,即加载一个项目的列表,并且我们希望将其设为选定项目。

    <ComboBox ItemsSource="{Binding ComboData}"  Height="30"  Width="300" Loaded="OnLoaded" /> 
    

    好的,在页面后面的代码中,我们将 datacontext 设置为我们之前设置的 ViewModel,但也有一个 OnLoaded 方法来检查 1 项加载情况。在下面的示例中,我们模拟了加载其余数据的 3 秒延迟。

    public sealed partial class MainPage : Page
    {
    
        public MainVM ViewModel { get; set; }
    
        public MainPage()
        {
            this.InitializeComponent();
            DataContext = ViewModel = new MainVM();
        }
    
    
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var cBox = sender as ComboBox;
    
            if (cBox != null)
            {
                if ((cBox.Items != null) && (cBox.Items.Count == 1))
                {
                    cBox.SelectedIndex = 0;
    
                    // Debug code to simulate a change
                    Task.Run(() =>
                        {
                            // Sleep 3 seconds
                            new System.Threading.ManualResetEvent(false).WaitOne(3000);
    
                            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                                { ViewModel.ComboData = new List<string> {"Alpha", "Gamma", "Omega"}; });
    
                        });
    
                }
            }
        }
    
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
    

    【讨论】:

    • 是的,我知道,但请告诉我如何在我的 Windows 8 应用程序中使用类似属性的占位符。
    猜你喜欢
    • 2014-03-08
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2020-05-27
    • 2023-04-08
    • 2014-11-27
    • 1970-01-01
    相关资源
    最近更新 更多