嗨我需要一个WPF应用程序的功能类似于asp.net中的autopostback选项。发生的事情是我有一个带有由xml文件填充的组合框的表单。因此,一旦用户选择“其他”作为选项,就会看到文本框和按钮。所以我的代码有什么问题吗?我使用比较字符串方法来实现该功能,但似乎它不起作用。
private void comboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
if (comboBox1.SelectedValue.ToString() == "Others")
{
BuilderemailTextBox.Visibility = Visibility.Visible;
BuilderupdateButton.Visibility= Visibility.Visible;
}
else
{
BuilderemailTextBox.Visibility = Visibility.Hidden;
BuilderupdateButton.Visibility = Visibility.Hidden;
}
}
我也尝试了以下操作,也对SelectedIndex进行了更改。
private void comboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
if (comboBox1.SelectedIndex.ToString() == "Others")
{
BuilderemailTextBox.Visibility = Visibility.Visible;
BuilderupdateButton.Visibility= Visibility.Visible;
}
else
{
BuilderemailTextBox.Visibility = Visibility.Hidden;
BuilderupdateButton.Visibility = Visibility.Hidden;
}
}
编辑1:我的XAML文件:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="309" Width="672">
<Grid>
<Button Height="23" Name="BuildButton" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" Click="BuildButton_Click">Build</Button>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,63,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox1_SelectedIndexChanged" />
<CheckBox Height="16" HorizontalAlignment="Left" Margin="66,0,0,140" Name="ExecbuildstartingmailCheckBox" VerticalAlignment="Bottom" Width="153">Exec Build Starting Mail</CheckBox>
<ComboBox Height="23" Margin="0,63,173,0" Name="comboBox2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" />
<Button Height="23" Margin="270,63,0,0" Name="BuilderupdateButton" VerticalAlignment="Top" HorizontalAlignment="Left" Width="51" Visibility="Hidden">Button</Button>
<Button Height="23" Margin="0,63,73,0" Name="button2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="74">Button</Button>
<TextBox Height="23" HorizontalAlignment="Left" Margin="144,63,0,0" Name="BuilderemailTextBox" VerticalAlignment="Top" Width="120" Visibility="Hidden" />
</Grid>
</Window>
edit2我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<value>builder@example.com</value>
</builder>
<builder>
<value>Others</value>
</builder>
</builderemail>
<manageremail>
<manager>
<value>manager@example.com</value>
</manager>
<manager>
<value>Others</value>
</manager>
</manageremail>
</email>
答案 0 :(得分:0)
根据您的XML,您可以使用SelectedItem
,投射它并访问其属性。如果您的商品的XmlElement
名称为value
,您要为“其他”进行测试,则可以尝试以下操作:
(comboBox1.SelectedItem as XmlElement).GetElementsByTagName("value")[0].InnerText == "Other"