WPF中基于组合框项目的文本框文本 [英] Text of Text Box Based on ComboBox Item in WPF

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

问题描述

我的应用程序中有一个ComboBox,其中ComboBoxItems为是"和否".

如果所选的ComboBoxItem为是",我想将我的文本框的文本分配为已清除",如果所选的ComboBoxItem为否",则将我的文本框的文本分配为未清除".

I have a ComboBox in my application in which ComboBoxItems are "Yes" and "No".

I want to assign the text of my TextBox as "cleared" if if the selected ComboBoxItem is "Yes" and "not cleared" if the selected ComboBoxItem is "No". How do I do that in WPF?

推荐答案

假设第一个项目为是",第二个项目为否",则应执行以下操作:

Assuming first item is "Yes" and second "No", you do something like this:

MyComboBox.SelectionChanged += (sender, eventArgs) => {
    if (cb.SelectedIndex == 0)
        this.MyTextBox.Text = "Cleared";
    else
        this.MyTextBox.Text = "No, not cleared!";
}; //MyComboBox.SelectionChanged



就这样.

—SA



That''s it.

—SA


您可以使用DataTrigger进行此操作:
You can do this with a DataTrigger:
<Grid>
    <ComboBox VerticalAlignment="Top" HorizontalAlignment="Left" SelectedIndex="0" Name="cboBool">
        <ComboBoxItem>Yes</ComboBoxItem>
        <ComboBoxItem>No</ComboBoxItem>
    </ComboBox>
    <TextBox VerticalAlignment="Bottom">
        <TextBox.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=cboBool, Path=SelectedValue.Content}" Value="Yes">
                        <Setter Property="TextBox.Text" Value="Cleared" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=cboBool, Path=SelectedValue.Content}" Value="No">
                        <Setter Property="TextBox.Text" Value="Not Cleared" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</Grid>


您在寻找VisualChild吗?

http://www.dotnetfunda.com/codes/code821-find-a- childelement-from-dependencyobject.aspx [ ^ ]

检查一下.:rose:
Are you looking for VisualChild?

http://www.dotnetfunda.com/codes/code821-find-a-childelement-from-dependancyobject.aspx[^]

Check this.:rose:


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

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