总是得到错误 - “对象引用未设置为对象的实例”。

时间:2013-07-16 02:50:44

标签: c#-4.0 wpf-controls contextmenu wpf-4.0 menu-items

我试过这种方式......所以当我点击button1中的任何menuitem时,它会检查并检查第二个按钮中的相同menuitem ...但是有些地方出现错误......我想写得更好方式。所以它将工作,因为我需要没有任何issuse.here是以下代码我到目前为止...我怎么能解决这个问题以及如何改进这个编码... 这是我的代码..

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Orientation="Horizontal" FlowDirection="LeftToRight">
        <Button  Name="btnPhone" Content="Business"   Click="OnbtnPhoneClick" ></Button>

        <Button Name="ddBtnPhone" Width="25"  Click="OnddBtnPhoneClick" Height="47">
            <Button.Content>
                <Path x:Name="btnArrow1" Margin="4" VerticalAlignment="Center" Width="10" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
            </Button.Content>
            <Button.ContextMenu>
                <ContextMenu x:Name="cmPhone">
                    <MenuItem Header="Assistant " Name="mnIAssistant" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>                                                    <MenuItem Header="Business" Name="mnIBusiness" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>                                                    <MenuItem Header="Business 2 " Name="mnIBusiness2" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Business FAX " Name="mnIBusinessFAX" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Call Back" Name="mnICallback" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Car" Name="mnICar" Click="OnMenuItemChecked"/>

                </ContextMenu>
            </Button.ContextMenu>
        </Button>
        <TextBox Height="19" Name="textBox1" Width="120" />
    </StackPanel>
    <StackPanel Grid.Row="2" Orientation="Horizontal" FlowDirection="LeftToRight">
        <Button  Name="btnHome" Content="Home" Click="OnbtnHomeClick"  Height="26" Width="64" />

        <Button Name="ddBtnHome" Width="25"  Click="OnddBtnHomeClick" Margin="0,0,0,5"  >
            <Button.Content>
                <Path x:Name="btnArrow2" Margin="4" VerticalAlignment="Center" Width="10" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
            </Button.Content>
            <Button.ContextMenu>
                <ContextMenu Name="cmHome">
                    <MenuItem Header="Assistant " Name="mnIAssistant1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Business" Name="mnIBusiness1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Business 2 " Name="mnIBusiness21" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Business FAX " Name="mnIBusinessFAX1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Call Back" Name="mnICallback1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
                    <MenuItem Header="Car" Name="mnICar1" Click="OnMenuItemChecked"/>

                </ContextMenu>
            </Button.ContextMenu>
        </Button>
        <TextBox Height="23" Name="textBox2" Width="120" />
    </StackPanel>

    <Border Grid.Row="3" BorderThickness="2" BorderBrush="Blue">
        <TextBlock Name="tbWebpage" Grid.Row="3" Background="White" VerticalAlignment="Top" Height="20">
                                    <Hyperlink NavigateUri="tbWebpage.Text" RequestNavigate="Hyperlink_RequestNavigate"></Hyperlink>
                                </TextBlock>
    </Border>

</Grid>
xaml背后的代码

     namespace MenuItemsChecked
    {

   public partial class MainWindow : Window
   {
    //string[] arrMenitems = { "business", "a1", "a2", "a3","a4" };
    Button btnCommon = new Button();
    TextBox txtCommon = new TextBox();
    MenuItem mnItem;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void AddMenuItems(ContextMenu cMenu, Button btnName, TextBox txtBoxName)
    {
       //mnItem.Items.Add(arrMenitems);
        if (cMenu.Items.Count != 0)
        {
            //mnItem = new MenuItem();
            //mnItem.Header = "Business";
            //mnItem.IsCheckable = true;
            //cMenu.Items.Add(mnItem);
            //mnItem = new MenuItem();
            //mnItem.Header = "Assistant";
            //mnItem.IsCheckable = true;
            //cMenu.Items.Add(mnItem);
            //mnItem = new MenuItem();
            //mnItem.Header = "Business 2";
            //mnItem.IsCheckable = true;
            //cMenu.Items.Add(mnItem);
            //mnItem = new MenuItem();
            //mnItem.Header = "Business FAX";
            //mnItem.IsCheckable = true;
            //cMenu.Items.Add(mnItem);
            //mnItem = new MenuItem();
            //mnItem.Header = "Call Back";
            //mnItem.IsCheckable = true;
            //cMenu.Items.Add(mnItem);
            //cMenu.Items.Add(mnItem);
            //cmCommon = cMenu;
            cMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
            cMenu.AddHandler(MenuItem.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseButtonUpEventClick));
           // cMenu.AddHandler(MenuItem.CheckedEvent, new RoutedEventHandler(OnMenuItemChecked));
            btnCommon = btnName;
            //txtCommon = txtBoxName;

        }

    }

    private void OnMenuItemChecked(object sender, RoutedEventArgs e)
    {
        RoutedEventArgs args = e as RoutedEventArgs;
        MenuItem item = args.OriginalSource as MenuItem;
        if (sender == mnIAssistant)
        {
            mnIAssistant.IsChecked = true;
            mnIAssistant1.IsChecked = true;
        }

        if (sender == mnIBusiness)
        {
            mnIBusiness.IsChecked = true;
            mnIBusiness1.IsChecked = true;
        }
        if (sender == mnIBusiness2)
        {
            mnIBusiness2.IsChecked = true;
            mnIBusiness21.IsChecked = true;
        }
        if (sender == mnIBusinessFAX)
        {
            mnIBusinessFAX.IsChecked = true;
            mnIBusinessFAX1.IsChecked = true;
        }
        if (sender == mnICallback)
        {
            mnICallback.IsChecked = true;
            mnICallback1.IsChecked = true;
        }
        if (sender == mnICar)
        {
            mnICar.IsChecked = true;
            mnICar1.IsChecked = true;
        }
        if (sender == mnIAssistant1)
        {
            mnIAssistant.IsChecked = true;
            mnIAssistant1.IsChecked = true;
        }
        if (sender == mnIBusiness1)
        {
            mnIBusiness.IsChecked = true;
            mnIBusiness1.IsChecked = true;
        }
        if (sender == mnIBusiness21)
        {
            mnIBusiness2.IsChecked = true;
            mnIBusiness21.IsChecked = true;
        }
        if (sender == mnIBusinessFAX1)
        {
            mnIBusinessFAX.IsChecked = true;
            mnIBusinessFAX1.IsChecked = true;
        }
        if (sender == mnICallback1)
        {
            mnICallback.IsChecked = true;
            mnICallback1.IsChecked = true;
        }
        if (sender == mnICar1)
        {
            mnICar.IsChecked = true;
            mnICar1.IsChecked = true;
        }



    }
    private void OnMouseButtonUpEventClick(object sender, MouseButtonEventArgs e)
    {
        (sender as MenuItem).IsChecked = !(sender as MenuItem).IsChecked;
        ((sender as MenuItem).Parent as ContextMenu).IsOpen = false;
    }


    private void OnMenuItemClick(object sender, RoutedEventArgs e)
    {
        RoutedEventArgs args = e as RoutedEventArgs;
       // MenuItem item = args.OriginalSource as MenuItem;
        //string header = item.Header.ToString();
        MenuItem menuitem = sender as MenuItem; ///always getting error-"Object reference not set to an instance of an object."
        //ContextMenu cm = sender as ContextMenu;
        string name = menuitem.Name;
        string header = menuitem.Header.ToString();
       if ((sender == mnIBusiness) |(sender == mnIBusiness1))
            {
                if (header == "Business")
                {

                mnIBusiness.IsChecked = true;
                mnIBusiness1.IsChecked = true;
              }
            btnCommon.Content = header;
            //item.IsCheckable = true;                

        }
        else if ((sender == mnIAssistant) | (sender == mnIAssistant1))
        {
            if (header == "Assistant")
            {
                mnIAssistant.IsChecked = true;
                mnIAssistant1.IsChecked = true;
            }
            btnCommon.Content = header;
            ///item.IsChecked = true;
        }
       else if ((sender == mnIBusiness2) | (sender == mnIBusiness21))
        {
           //if(header=="Business 2")
           //{
             mnIBusiness2.IsChecked = true;
            mnIBusiness21.IsChecked = true;
           //}
            btnCommon.Content = header;
            //item.IsChecked = true;
        }
       else if ((sender == mnIBusinessFAX) |(sender == mnIBusinessFAX1))
        {
           if(header=="Business FAX")
           {
               mnIBusinessFAX.IsChecked = true;
               mnIBusinessFAX1.IsChecked = true;
           }
            btnCommon.Content = header;
            //item.IsChecked = true;
        }
       else if ((sender == mnICallback1) | (sender == mnICallback))
       {
           if (header == "Call Back")
           {
               mnICallback.IsChecked = true;
               mnICallback1.IsChecked = true;
           }
           btnCommon.Content = header;
           //item.IsChecked = true;
       }
        else 
        {
            if (header == "Car")
            {
                mnICar.IsChecked = true;
                mnICar1.IsChecked = true;
            }
            btnCommon.Content = header;

        }

        txtCommon.Text = string.Empty;
    }

    private void OnddBtnPhoneClick(object sender, RoutedEventArgs e)
    {
        ButtonContextMenuDisplay(sender);
        AddMenuItems(cmPhone, btnPhone, textBox1);
    }

    private void OnbtnPhoneClick(object sender, RoutedEventArgs e)
    {

    }

    private void OnbtnHomeClick(object sender, RoutedEventArgs e)
    {

    }

    private void OnddBtnHomeClick(object sender, RoutedEventArgs e)
    {
        ButtonContextMenuDisplay(sender);
        AddMenuItems(cmHome, btnHome, textBox2);
    }

    private void ButtonContextMenuDisplay(object sender)
    {
        (sender as Button).ContextMenu.IsEnabled = true;
        (sender as Button).ContextMenu.PlacementTarget = (sender as Button);
        (sender as Button).ContextMenu.IsOpen = true;
        (sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
    }

}

}

1 个答案:

答案 0 :(得分:0)

嘿kida不知怎的,你的onmenuitemclick被调用不止一次并且它第一次运行良好但是当它再次被调用时,menuitem变为null所以它给你null引用异常我已经放了一个小的支票...来做它工作......所以检查出来..

 private void OnMenuItemClick(object sender, RoutedEventArgs e)
    {
        RoutedEventArgs args = e as RoutedEventArgs;
        // MenuItem item = args.OriginalSource as MenuItem;
        //string header = item.Header.ToString();
        MenuItem menuitem = sender as MenuItem; ///always getting error-"Object reference not set to an instance of an object."

        if (menuitem != null)
        {
            ContextMenu cm = sender as ContextMenu;
            //    string name = menuitem.Name;
            string header = menuitem.Header.ToString();
            if ((sender == mnIBusiness) | (sender == mnIBusiness1))
            {
                if (header == "Business")
                {

                    mnIBusiness.IsChecked = true;
                    mnIBusiness1.IsChecked = true;
                }
                btnCommon.Content = header;
                //item.IsCheckable = true;                

            }
            else if ((sender == mnIAssistant) | (sender == mnIAssistant1))
            {
                if (header == "Assistant")
                {
                    mnIAssistant.IsChecked = true;
                    mnIAssistant1.IsChecked = true;
                }
                btnCommon.Content = header;
                ///item.IsChecked = true;
            }
            else if ((sender == mnIBusiness2) | (sender == mnIBusiness21))
            {
                //if(header=="Business 2")
                //{
                mnIBusiness2.IsChecked = true;
                mnIBusiness21.IsChecked = true;
                //}
                btnCommon.Content = header;
                //item.IsChecked = true;
            }
            else if ((sender == mnIBusinessFAX) | (sender == mnIBusinessFAX1))
            {
                if (header == "Business FAX")
                {
                    mnIBusinessFAX.IsChecked = true;
                    mnIBusinessFAX1.IsChecked = true;
                }
                btnCommon.Content = header;
                //item.IsChecked = true;
            }
            else if ((sender == mnICallback1) | (sender == mnICallback))
            {
                if (header == "Call Back")
                {
                    mnICallback.IsChecked = true;
                    mnICallback1.IsChecked = true;
                }
                btnCommon.Content = header;
                //item.IsChecked = true;
            }
            else
            {
                if (header == "Car")
                {
                    mnICar.IsChecked = true;
                    mnICar1.IsChecked = true;
                }
                btnCommon.Content = header;

            }

            txtCommon.Text = string.Empty;
        }
    }

在评论中发布您的查询..