本文介绍了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?

推荐答案

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


<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>



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

08-30 07:41