我正在使用Windows应用程序,您可以在其中阻止和允许程序。我对如何调用comboboxitem有问题。 inc是组合框1内的一个项目,所有都是组合框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>




这是程序的屏幕截图
enter image description here

最佳答案

更新

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

我认为我发现了您的问题,在您的if条件下,这种情况永远不会发生,因为inc和全部都是<ComboBox x:Name="Combobox1"的一部分,并且在您的代码中您说的是if ( inc.SelectedIndex > -1 && all.SelectedIndex > -1)不能同时选择这两个;)

例如,这应该起作用,因为它具有OR运算符if (outg.Checked || all.Checked),因此应仅选择其中之一。

忽略以下部分,是一个解决方案,认为您正在使用Win Forms

您可以使用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);
        }


上面的代码示例,运行:

c# - C#Combobox调用comboboxitem-LMLPHP

关于c# - C#Combobox调用comboboxitem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47236295/

10-13 08:01