简体   繁体   English

WPF中基于组合框项目的文本框文本

[英]Text of Text Box Based on ComboBox Item in WPF

我在应用程序中使用一个组合框,其中的组合框项目为“是”和“否”。如果要选择组合框项目,则要将文本框的文本分配为“已清除”,如果选择了组合框项目“否”,则将其文本框分配为“未清除”。我在WPF上这样做

If you want to do it entirely in XAML, you could use an element binding on the TextBox (to the checkbox/combobox), and then implement a value converter to convert the Yes/No to the appropriate string. 如果要完全在XAML中完成此操作,则可以在TextBox上使用元素绑定绑定到复选框/组合框),然后实现一个值转换器 ,将Yes / No转换为适当的字符串。

Alternatively, if using MVVM, you could bind the checkbox IsChecked or combobox SelectedValue to a property on your view model, and in that properties setter, notify another property which is your text box text, which has just a getter which returns the appropriate string based on your first view model property. 或者,如果使用MVVM,则可以将复选框IsChecked或组合框SelectedValue绑定到视图模型上的属性,然后在该属性设置器中,通知另一个属性,即您的文本框文本,该属性只有一个getter,该属性将根据以下内容返回适当的字符串:在您的第一个视图模型属性上。 Bind your TextBox Text property to this new view model property. 将您的TextBox Text属性绑定到此新视图模型属性。

Create an implementation of IValueConverter 创建IValueConverter的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace XAMLConverter
{
    public class ComboBoxConverter : IValueConverter
    {


        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {                
                if (value.ToString() == "Yes")
                    return "Cleared";
                else if (value.ToString() == "No")
                    return "Not Cleared";
                else
                    return "";

            }
            catch
            {
                return "";
            }
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Add your namespace to the XAML for: 将您的名称空间添加到XAML中,用于:

xmlns:conv="clr-namespace:XAMLConverter"

Add a resource for the converter: 为转换器添加资源:

<Window.Resources>        
    <conv:ComboBoxConverter x:Key="ComboBoxConverter" />        
</Window.Resources>

Then add your controls: 然后添加您的控件:

    <StackPanel>
        <ComboBox Name="SelectControl">
            <ComboBoxItem Content="Yes" />
            <ComboBoxItem Content="No" />
        </ComboBox>

        <TextBox Text="{Binding ElementName=SelectControl, 
                                Path=SelectedItem.Content, 
                                Converter={StaticResource ComboBoxConverter}}" 
        />

    </StackPanel>

Trigger solution goes here: 触发解决方案在这里:

        <ComboBox Name="cb">
            <ComboBoxItem>Yes</ComboBoxItem>
            <ComboBoxItem>No</ComboBoxItem>
        </ComboBox>
        <TextBox>
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=cb, Path=SelectedItem.Content}" Value="Yes">
                            <Setter Property="Text" Value="cleared"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=cb, Path=SelectedItem.Content}" Value="No">
                            <Setter Property="Text" Value="not cleared"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM