wpf 根据组合框所选项目更改文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21934958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change textbox value depending on combobox selected item
提问by Offer
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'. If Gender has 'Female' selected then sex.Text should show '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. E.g when I select female it displays 'm' and vice-verse, as if it's having a case of delayed action.
这个想法是,如果 Gender 组合框选择了“Male”,那么 sex.Text 应该显示“m”。如果 Gender 选择了“Female”,则 sex.Text 应显示“f”。但不幸的是,性别文本框在第一个选择中不会显示任何内容。但后来,它一直显示出与我想要的相反的东西。例如,当我选择女性时,它显示“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.
非常感谢帮助。
回答by Samuel Dambroso
Okay, I can't add a coment, so here go a suggestion.
好吧,我无法添加评论,所以这里有一个建议。
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.
我只是在这里编码,很抱歉任何输入错误。
回答by Sheridan
If you want to write WPF properly, then you reallyneed to learn 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 DataTriggers:
如果你想正确地编写WPF,那么你真的需要学习XAML。您不需要任何代码来执行您想要的操作(当然数据属性除外)。你只需几个DataTriggers就可以做到:
<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 DependencyPropertyto 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); }
}
回答by Kenny Liard
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";
}
}
回答by vivat pisces
Assuming your items in the ComboBoxare 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 ComboBoxreflect the new selection when the event fires, but the Textproperty still reflects the previous selection.
当事件触发时,似乎Selected-type 属性ComboBox反映了新的选择,但该Text属性仍然反映了以前的选择。

