0

I'm writing a application in C# WPF.

I am having a small issue here.

In the class.cs, I have a line:

MessageBox.Show("Cancelled sending !");

And on my form I have a radioButton1.

How can I make it like

if (radioButton1.IsChecked == true)
{
    MessageBox.Show("Cancelled sending !");
}

Because when I try it, it doesn't find the radioButton1

I have tried it different ways, but I can't find how to do it.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
user2944342
  • 105
  • 2
  • 2
  • 10

4 Answers4

0

If I understand you correct, you are trying to access the value of your radioButton in your class.cs, right? If so, you can make a public property, which encapsulates your radiobutton-checked-property.

This can look like:

public bool IsRadioButtonChecked
{
   get{return radioButton1.IsChecked;}
}

In your class.cs you need an instance of the window.

Tomtom
  • 9,087
  • 7
  • 52
  • 95
0

The radio button that you created on your form is stored in a private variable in the MainForm class. Therefore, it is inaccessible from other classes.

You could turn it into a public variable, but this is not a very good approach, and in any case, will be overwritten by the Forms designer as you add/remove/resize controls on the form.

The better solution would be to pass the reference to the radio button to your other class, either via the constructor, or as an argument to the method that is checking the status of the radio button (if it is invoked by the form).

For example:

public class MyClass {
    private mRbutton;

    public MyClass(RadioButton rbutton) {
        mRbutton = rbutton;
        // Rest of the construction code...
    }

    //
    // ... Rest of the class code ...
    //

    public void MessageShowingMethod() {
        if (mRbutton.IsChecked == true) {
            MessageBox.Show("Cancelled sending !");
        }
    }
}
Mr. 47
  • 76
  • 4
  • If he was doing a windows form project this would be correct. Mind you, sure it will work in wpf just as well, but its not really in line with the whole mvvm thing, is it? – hschne Nov 28 '13 at 12:50
0

Try this maybe. In your main form:

if (radioButton1.IsChecked == true)
{
  int checked = 1;
}

callMyMethod(checked);

And then in your class.cs:

public callMyMethod(int checked)
{
  if (checked == 1)
  {
    MessageBox.Show("Cancelled sending !");
  }
  else
  {
    MessageBox.Show("Something else");
  }
}
J2D8T
  • 827
  • 11
  • 24
0

When using WPF, we have Window and UserControl classes, among other things. These are partial classes, which means that they have more than one file in them. There is a .xaml file where we define the UI and a code behind .xaml.cs file where we can access the named UI elements that we defined in the related .xaml file. You should never access a UI element in any other class.

To access a UI element in the code behind file, you must first name it in the .xaml file:

<RadioButton Name="RadioButton" />

Then, in the code behind file only, you can access it using that name:

if (RadioButton.IsChecked)
{
    MessageBox.Show("Cancelled sending !");
}

Once again, you should never access a UI element in any other class.

Sheridan
  • 68,826
  • 24
  • 143
  • 183