C#Combobox调用comboboxitem

C#Combobox调用comboboxitem,c#,C#,我正在开发一个Windows应用程序,您可以在其中阻止和允许程序。我对如何调用comboboxitem有问题。inc是组合框1内的一个项目,all是组合框2内的一个项目。我想创建一个条件,如果两个comboboxitem都被选中,则运行此操作。我该怎么做?多谢各位 private void addSubmitBtn_Click(object sender, RoutedEventArgs e) { foreach (string path in pathT.T

我正在开发一个Windows应用程序,您可以在其中阻止和允许程序。我对如何调用comboboxitem有问题。inc是组合框1内的一个项目,all是组合框2内的一个项目。我想创建一个条件,如果两个comboboxitem都被选中,则运行此操作。我该怎么做?多谢各位

      private void addSubmitBtn_Click(object sender, RoutedEventArgs e)
    {
        foreach (string path in pathT.Text.Split(';'))
            if (File.Exists(path))
            {
                if ( inc.SelectedIndex > -1 && all.SelectedIndex > -1)
                {
                    FWRule(path, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN, (blo.Checked) ? NET_FW_ACTION_.NET_FW_ACTION_BLOCK : NET_FW_ACTION_.NET_FW_ACTION_ALLOW, ((Control)sender).Tag.ToString());
                }
                if (outg.Checked || all.Checked)
                {
                    FWRule(path, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT, (blo.Checked) ? NET_FW_ACTION_.NET_FW_ACTION_BLOCK : NET_FW_ACTION_.NET_FW_ACTION_ALLOW, ((Control)sender).Tag.ToString());
                }
            }
        ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
    }

    private void FWRule(string path, NET_FW_RULE_DIRECTION_ d, NET_FW_ACTION_ fwaction, string action)
    {
        try
        {
            INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
            Type.GetTypeFromProgID("HNetCfg.FWRule"));
            firewallRule.Action = fwaction;
            firewallRule.Enabled = true;
            firewallRule.InterfaceTypes = "All";
            firewallRule.ApplicationName = path;
            firewallRule.Name = "PRST: " + System.IO.Path.GetFileName(path);
            INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            firewallRule.Direction = d;
            if (action == "1") firewallPolicy.Rules.Add(firewallRule);
            else
            {
                ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
            }
        }
        catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "ERROR"); }
    }

    private void addCancelBtn_Click(object sender, RoutedEventArgs e)
    {
        ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
    }

    private void addBackBtn_Click(object sender, RoutedEventArgs e)
    {
        ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
    }

    private void pathT_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void blo_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void al_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog fd = new OpenFileDialog();
        fd.Filter = "Executable|*.exe";
        fd.Multiselect = true;
        if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pathT.Text = String.Join(";", fd.FileNames);
        }
    }


}
}

这是XAML部分

    <UserControl x:Class="WpfDeepTest.Views.policyAddFunc"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
         xmlns:local="clr-namespace:WpfDeepTest.Views"
         mc:Ignorable="d" 
         d:DesignHeight="700" d:DesignWidth="960">


<Grid Background="{DynamicResource MaterialDesignPaper}">
    <Grid.RowDefinitions>
        <RowDefinition Height=".5*"/>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height=".5*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="20 0 0 0">
        <Button Name="addBackBtn" Style="{DynamicResource MaterialDesignFloatingActionButton}" Width="40" Height="40" Click="addBackBtn_Click">
            <md:PackIcon Kind="ChevronLeft" Height="30" Width="30"/>
        </Button>
    </Grid>

    <md:Card Grid.Row="1" Margin="15 10" Padding="100 0" Height="400">
        <StackPanel VerticalAlignment="Center">
            <Grid Margin="0 0 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="1.5*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Application Path: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0"/>
                <TextBox Name="pathT" Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" md:HintAssist.Hint="Ex: InboundPolicy#1" Padding="5 0" Margin="0,0,57.2,10.2" TextChanged="TextBox_TextChanged_1"/>
                <Button Content="..." Grid.Column="1" HorizontalAlignment="Left" Margin="372,-1,0,0" VerticalAlignment="Top" Width="47" Height="36" Click="Button_Click"/>
            </Grid>

            <Grid Margin="0 30 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="1.5*"/>
                </Grid.ColumnDefinitions>

            </Grid>

            <Grid Margin="0 30 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="280*"/>
                    <ColumnDefinition Width="138*"/>
                    <ColumnDefinition Width="13*"/>
                    <ColumnDefinition Width="31*"/>
                    <ColumnDefinition Width="66*"/>
                    <ColumnDefinition Width="171*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Directions: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0" Margin="0,-25,0,25"/>
                <ComboBox x:Name="Combobox1"  Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" Padding="5 0" Margin="0,-31,0.2,39" Grid.ColumnSpan="5">
                    <ComboBoxItem Name="all" Content="All" HorizontalAlignment="Left" Width="417.6"/>
                    <ComboBoxItem  Name="inc" IsSelected="True" >OutBound</ComboBoxItem>
                    <ComboBoxItem Name="outg">InBound</ComboBoxItem>
                </ComboBox>
            </Grid>

            <Grid Margin="0 30 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="280*"/>
                    <ColumnDefinition Width="108*"/>
                    <ColumnDefinition Width="61*"/>
                    <ColumnDefinition Width="163*"/>
                    <ColumnDefinition Width="87*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Action: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0" Grid.ColumnSpan="2" Margin="2,-34,106,32.4"/>
                <ComboBox x:Name="Combobox2" SelectedItem="" Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" Padding="5 0" Margin="2,-30,-1.8,36.4" Grid.ColumnSpan="4">
                    <ComboBoxItem Name="blo" IsSelected="True">Block</ComboBoxItem>
                    <ComboBoxItem Name="al">Allow</ComboBoxItem>
                </ComboBox>
            </Grid>
        </StackPanel>
    </md:Card>

    <StackPanel Orientation="Horizontal" Grid.Row="2" HorizontalAlignment="Center">
        <Button Name="addSubmitBtn" Style="{DynamicResource MaterialDesignRaisedButton}" VerticalAlignment="Top" Margin="10 20" Content="Submit" Width="150" Height="35" TabIndex="1" Click="addSubmitBtn_Click"/>
        <Button Name="addCancelBtn" Style="{DynamicResource MaterialDesignRaisedLightButton}" VerticalAlignment="Top" Margin="10 20" Content="Cancel" Width="150" Height="35" Click="addCancelBtn_Click"/>
    </StackPanel>
</Grid>

出境
入境
块
容许

下面是该程序的屏幕截图

更新

可以选择组合框项目,但不能选中,对吗?您应该检查它们是否仅被选中。除非您构建自己的控件,该控件具有可选中的复选框项组合框


我想我发现了您的问题,在您的if条件下,这种情况永远不会发生,因为inc和all都是
-1&&all.SelectedIndex>-1)的一部分。
不能同时选择这两个;)

例如,这应该是可行的,因为它有一个OR运算符
,如果(outg.Checked | | all.Checked)
,则只应选择其中一个

忽略下面的部分,这是一个解决方案,认为您正在使用Win表单 您可以使用
selectedIndexChanged

例如:

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.getValues(this.comboBox2, this.comboBox3);

        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.getValues(this.comboBox2, this.comboBox3);
        }

        public void compareValues(String value1, String value2)
        {
            if(value1.Equals("inc") && value2.Equals("all"))
            {
                this.runThis();
            }
        }

        public void runThis()
        {
            //do your thing here :)
            this.label1.Text = "run this!!!";
        }

        public void getValues(ComboBox cmb2, ComboBox cmb3)
        {
            String cmb1Value = "";
            String cmb2Value = "";

            try{
                cmb1Value = this.comboBox2.SelectedItem.ToString();
            }catch (Exception exception){
                //not both set
            }

            try
            {
                cmb2Value = this.comboBox3.SelectedItem.ToString();
            }catch (Exception exception){
                //not both set
            }

            this.compareValues(cmb1Value, cmb2Value);
        }
上述代码的示例,正在运行:


谢谢您的回复。你能看到我的代码有什么问题吗?我马上去检查一下……只给我几分钟;)这不是我最初的编码,我把它改为selectedIndex,因为有人建议这样做。但是他删除了他的评论。请你和我分享你的原始代码,以及你声明inc的地方,我需要查看他们的声明和集成。然后我可以提供一个完整的示例,说明我编辑的原始代码的用法。这就是全部编码我想我发现了你的问题,在你的if条件下,这种情况永远不会发生,因为inc和all都是
-1&&all.SelectedIndex>-1)的一部分。
这两个不能同时选择;)例如,这应该是可行的,因为它有一个OR运算符
,如果(outg.Checked | | all.Checked)
,则只应选择其中一个。