0
votes

Right now I have code where the user can click on the canvas and it places a yellow node, and then a window pops up asking for information about that node. I'm trying to make it so the user has to first select an option from two choices on a menu, and then they are able to click on the canvas. I can't seem to figure this out and I'm sure it's something simple. I have a MouseButtonEventArgs method for the pressing down, but I can't tie that into the RoutedeventArgs method for clicking on the menu option. Any tips?

So basically I'm wondering if there's a way to make it so a MouseButtonEventArgs method can't be used unless a RoutedEventArgs method is selected first. I.E. The user can't click on the canvas (addNode_MouseDown(object sender, MouseButtonEventArgs e), unless a menu option is selected(clicked) first (meunMethod_Here(object sender, RoutedEventArgs e).

Here's my method right now for adding the node:

private void addNode_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Point currentPoint = new Point();
        if (e.ButtonState == MouseButtonState.Pressed)
            currentPoint = e.GetPosition(this);

        Ellipse ellipse = new Ellipse();

        SolidColorBrush mySolidColorBrush = new SolidColorBrush();
        mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);

        ellipse.Fill = mySolidColorBrush;
        ellipse.Width = 10;
        ellipse.Height = 10;

        Canvas.SetLeft(ellipse, e.GetPosition(canvas1).X);
        Canvas.SetTop(ellipse, e.GetPosition(canvas1).Y);
        canvas1.Children.Add(ellipse);

        InputPopupWindow popup = new InputPopupWindow();
        popup.ShowDialog(); 
    }

XAML:

<Window x:Class="***.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="***" Height="410" Width="869">
<Grid Height="387">
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_Browse File" Click="BrowseButton_Click" />
            <MenuItem Header="_Add" >
                <MenuItem Header="_Known Object" />
                <MenuItem Header="_Waypoint" />
            </MenuItem>
        </Menu>
    </DockPanel>
    <Canvas Margin="0,21,0,7" x:Name="canvas1" MouseDown= "addNode_MouseDown" HorizontalAlignment="Left" Width="637"></Canvas>
    <Grid Name="inputPopup" MouseDown="addNode_MouseDown" HorizontalAlignment="Right" Width="210" Margin="0,21,0,0">
        <Grid.ColumnDefinitions>
        <Border BorderBrush="Black" BorderThickness="3" Grid.ColumnSpan="2">
            <Grid Margin="0, 40, 0, 20" Background="White" Height="303">
            </Grid>
        </Border>
    </Grid>
</Grid>
</Window>
1
Try to make it clear how your question is relevant to more people than just you. If it's not, then please delete it.franssu
Alright I edited it.pfinferno

1 Answers

0
votes

I created two separate methods to check if each of the options was selected using IsChecked, and subscribed each selection to its respective method.

Example:

<MenuItem Header="_Add" >
                <MenuItem Header="_Known Object" x:Name="known_Object"  IsCheckable="False" Click="addKnown_Object" />

Code Behind:

private void addKnown_Object(object sender, RoutedEventArgs e)
    {
        known_Object.IsChecked = true;
    }

Then the method that actually places the nodes first checks to see if the .IsChecked of the respective selection is true.

If there's an easier/simpler way to do this though let me know!