WPF组合框中的默认文本 [英] wpf default text in combobox

查看:78
本文介绍了WPF组合框中的默认文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道如何在页面加载时在组合框中设置默认文本.在我的应用程序中,我定制了具有不同语言和页面加载时的组合框,我想将文本显示为选择"

<字体大小= 2 color =#0000ff"> <字体大小= 2 color =#0000ff">

<

Hi,

I want to know how to set default text in combobox on pageload.In my application i have customized combobox which has different languages and on page load i want to display text as 'select language'.

<

组合框 IsSynchronizedWithCurrentItem ="True" x : 名称 =" cbLanguages" VerticalAlignment =居中" 高度 =自动"

ComboBox IsSynchronizedWithCurrentItem="True" x:Name="cbLanguages" VerticalAlignment="Center" Height="Auto"

 

FontSize ="20" 水平对齐 =拉伸" 宽度 =自动" 边距 ="" 0,0,20,0" Canvas.Top ="3.5" Canvas.Left ="4.5"

FontSize="20" HorizontalAlignment="Stretch" Width="Auto" Margin="0,0,20,0" Canvas.Top="3.5" Canvas.Left="4.5"

 

样式 ='"{ DynamicResource ComboBoxStyle }" ItemsSource =" { 绑定 路径 =支持的文化, 模式 = OneWay 来源 = { StaticResource CultureResourcesDS }}>

cblanguages.text =选择语言"不起作用.如果我将其设置为raedonly且可编辑,则该自定义外观不会出现.

我希望此文本始终显示.当用户单击组合框时,它应该显示语言列表,选择一种语言后,应再次显示选择的语言文本.

请帮助我.

Style="{DynamicResource ComboBoxStyle}" ItemsSource="{Binding Path=SupportedCultures, Mode=OneWay, Source={StaticResource CultureResourcesDS}}">

cblanguages.text="Select language" is not working.If i make it raedonly and editable then the customized look of that is not coming.

I want this text to be displayed always.when the user clicks on the combobox then it should display the list of languages and after selecting one it should again display select langauge text.

Please help me.

推荐答案

首先,您可以将默认文本添加为​​语言列表中的第一项.其次,在SelectionChanged回调中,在系统空闲时将默认文本分配给combobox Text属性.请参见下面的示例:

标记:
< Window x:Class =" WpfApplication20.Window1"
xmlns =" xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml "
" Title ="Window1"高度="300".宽度="300">
< StackPanel>
< ComboBox Name ="lstProducts" ItemsSource ="{Binding}" DisplayMemberPath =名称";文字="{{绑定名称}"" SelectionChanged ="lstProducts_SelectionChanged"/>
</StackPanel<
</Window>

代码:
使用系统;
使用系统.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Threading;

First, you could add the default text to be the first item in the language list. Second, in SelectionChanged callback, assign the default text to the combobox Text property when the system is idle.  Please see example below:

Markup:
<Window x:Class="WpfApplication20.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ComboBox Name="lstProducts" ItemsSource="{Binding}" DisplayMemberPath="Name" Text="{Binding Name}" SelectionChanged="lstProducts_SelectionChanged"/>
    </StackPanel>
</Window>

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

命名空间WpfApplication20
{
////< summary>
///Window1.xaml的交互逻辑
///</summary>
public局部类Window1:Window
{
public Window1()
{InitializeComponent();
语言=新列表< Language>(){new Language() {Name = defaultText}} .Concat(new Languages());
DataContext =语言;
}

namespace WpfApplication20
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            languages = new List<Language>() { new Language() { Name = defaultText } }.Concat(new Languages());
            DataContext = languages;
        }

IEnumerable< Language>语言;
字符串defaultText =选择语言";

        IEnumerable<Language> languages;
        string defaultText = "Select language";

私有void lstProducts_SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
动作aciton =()=> lstProducts.Text = defaultText;
Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,aciton);
}
}

        private void lstProducts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Action aciton = () => lstProducts.Text = defaultText;
            Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, aciton);
        }
    }

公共类语言
{
公共字符串名称{set;得到; }

    public class Language
    {
        public string Name { set; get; }
    }

公共类语言:列表<语言>
{
public Languages()
Add(new Language(){Name ="English"});
Add(new Language(){Name ="Chinese"});
Add(new Language(){Name ="French"});
br/>}

    public class Languages : List<Language>
    {
        public Languages()
        {
            Add(new Language() { Name = "English" });
            Add(new Language() { Name = "Chinese" });
            Add(new Language() { Name = "French" });
        }
    }
}


这篇关于WPF组合框中的默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆