简体   繁体   English

根据组合框所选项目更改文本框值

[英]change textbox value depending on combobox selected item

I'm trying to make my c#, wpf application change the value in a textbox accordingly with the value in a combobox using 'IF' statements.我正在尝试使我的 c#、wpf 应用程序使用“IF”语句根据组合框中的值相应地更改文本框中的值。

The idea is that if the Gender combobox has 'Male' selected, then the sex.Text should show 'm'.这个想法是,如果 Gender 组合框选择了“Male”,那么 sex.Text 应该显示“m”。 If Gender has 'Female' selected then sex.Text should show 'f'.如果 Gender 选择了“Female”,则 sex.Text 应显示“f”。 But unfortunately the sex textbox will show nothing on the first selection.但不幸的是,性别文本框在第一个选择中不会显示任何内容。 But afterwards, it keeps showing the opposite of what I want it to.但后来,它一直显示出与我想要的相反的东西。 Eg when I select female it displays 'm' and vice-verse, as if it's having a case of delayed action.例如,当我选择女性时,它显示“m”,反之亦然,好像它有一个延迟动作的情况。

Here's the event:这是事件:

    private void gender_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (gender.Text == "Male")
        {
            sex.Text = "m";
        }
        if (gender.Text == "Female")
        {
            sex.Text = "f";
        }
    }

Any clue on how to make this work?关于如何使这项工作的任何线索?

I'm guessing this may have something to do with the winforms SelectedIndexChanged event being replaced by the SelectionChanged as it's wpf equivalant.我猜这可能与 winforms SelectedIndexChanged 事件被 SelectionChanged 替换有关,因为它与 wpf 等效。

Clearly I just may not know how to use it.显然,我可能不知道如何使用它。

Help much appreciated.非常感谢帮助。

Okay, I can't add a coment, so here go a suggestion. 好吧,我不能添加一个cament,所以这里提出一个建议。

Try to get the current value this way: 尝试以这种方式获取当前值:

private void gender_SelectionChanged(object sender, TextChangedEventArgs e)
 {
        var currentText = (sender as ComboBox).SelectedItem as string;

        if (currentText.Equals("Male"))
        {
            sex.Text = "m";
        }
        if (currentText.Equals("Female"))
        {
            sex.Text = "f";
        }
 }

I coded just here, so sorry for any mistyping. 我在这里编码,很抱歉任何错误输入。

If you want to write WPF properly, then you really need to learn XAML. 如果你想正确编写WPF,那么你真的需要学习XAML。 You don't need any code to do what you want (except for the data properties of course). 您不需要任何代码来执行您想要的操作(当然除了数据属性)。 You can do it with just a couple of DataTrigger s: 你可以用几个DataTrigger来做到这DataTrigger

<StackPanel>
    <ComboBox Name="ComboBox" ItemsSource="{Binding Items}" />
    <TextBox>
        <TextBox.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SelectedItem.Text, 
                        ElementName=ComboBox}" Value="Male">
                        <Setter Property="TextBox.Text" Value="m" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding SelectedItem.Text, 
                        ElementName=ComboBox}" Value="Female">
                        <Setter Property="TextBox.Text" Value="f" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</StackPanel>

This is of course assuming that you have a collection DependencyProperty to data bind to in the code behind: 这当然假设您在后面的代码中有一个集合DependencyProperty来绑定数据:

public static DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<Gender>), typeof(YourWindow));

public ObservableCollection<string> Items
{
    get { return (ObservableCollection<string>)GetValue(ItemsProperty); }
    set { SetValue(ItemsProperty, value); }
}

Assuming your items in the ComboBox are just strings, you could use this: 假设您在ComboBox中的项目只是字符串,您可以使用:

private void gender_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    sex.Text = gender.SelectedValue.Equals("Male") ? "m" : "f";
}

It looks like the Selected -type properties on the ComboBox reflect the new selection when the event fires, but the Text property still reflects the previous selection. 看起来ComboBox上的Selected -type属性反映了事件触发时的新选择,但Text属性仍然反映了之前的选择。

I would change your code to this: 我会将您的代码更改为:

private void gender_SelectionChanged(object sender, SelectionChangedEventArgs e)  
    {
        if (gender.SelectedValue == "Male")
        {
            sex.Text = "m";
        }
        if (gender.SelectedValue == "Female")
        {
            sex.Text = "f";
        }
    }

I am late to answer, but see the following to get the right coding:我来晚了,但请参阅以下内容以获得正确的编码:

In xaml page:在 xaml 页面中:

    <ComboBox x:Name="Gender" Grid.Row="0" Grid.Column="1" IsEditable="True" TextBoxBase.TextChanged="Gender_SelectionChanged"/>
    <TextBox  x:Name="Sex"    Grid.Row="0" Grid.Column="3"/>

In the C# page:在 C# 页面中:

    private void Gender_SelectionChanged(object sender, RoutedEventArgs e)
    {
        Sex.Text = Gender.Text.ToLower().Substring(0, 1);
    }

Good luck祝你好运

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

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