0

Say I have a WPF Form for managing the training and evaluation of employees for the Fork Lift (FL) and other equipment (using SQL/LINQ). I have it setup so when the check box for Training is checked, it makes the Textbox (for entering the date) and the check box for evaluation become Visible and vice versa.

Is there a way to make it so I can use a general "Checked" method based on which training check box I click as apposed to copy pasting it for each piece of equipment?

(FLT is Fork Life Training and FLE is Evaluation)

private void Checked(object sender, RoutedEventArgs e)
{
    FLT_txt.Visibility = Visibility.Visible;
    FLE_ck.Visibility = Visibility.Visible;
}

//Basically a reverse of the above, just here for example's sake
private void Unchecked(object sender, RoutedEventArgs e)
{
    FLT_txt.Visibility = Visibility.Hidden;
    FLE_ck.Visibility = Visibility.Hidden;
}

I hope I didn't make that confusing.

PRX
  • 137
  • 10
  • Don't manipulate UI elements in procedural code in WPF. Create a proper ViewModel for this and put your logic there. – Federico Berasategui Sep 19 '13 at 15:52
  • @HighCore, in my humblest opinion, you've got to give these new users a break every now and then. Although we think MVVM fits WPF perfectly, it's *not* mandatory. It seems a little bit unfair that your always barking this at new users. – Sheridan Sep 19 '13 at 15:58
  • @HighCore, I completely take it back (in this instance at least). Apparently, PRX *is* 'attempting' MVVM. – Sheridan Sep 19 '13 at 16:21
  • @Sheridan =). Still, I admit you're right. I have a very unpolite (is that even a word?) way of saying things – Federico Berasategui Sep 19 '13 at 16:23
  • I wasn't before. I'm pretty new to WinForm/WPF (student) so I've only seen the phrase View Model before :X. I'm researching MVVM now though :) Thanks – PRX Sep 19 '13 at 16:24
  • @HighCore, yes that is a word and apparently one that I myself am familiar with. :) – Sheridan Sep 19 '13 at 16:25
  • Apologies @PRX, my misunderstanding. – Sheridan Sep 19 '13 at 16:27
  • @HighCore any tips for getting started with MVVM? – PRX Sep 19 '13 at 21:04
  • @PRX [here](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137) and also [Rachel's Excellent Explanation here](http://stackoverflow.com/a/15684569/643085) – Federico Berasategui Sep 19 '13 at 21:15

2 Answers2

1

Are you talking about this:

<Grid ToggleButton.Checked="Grid_Checked">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <CheckBox Grid.Row="0" Content="1" />
    <CheckBox Grid.Row="1" Content="2" />
    <CheckBox Grid.Row="2" Content="3" />
</Grid>

private void Grid_Checked(object sender, RoutedEventArgs e)
{
    CheckBox checkBox = (CheckBox)e.Source;
    if (checkbox.Content = "1") DoSomething();
    else DoSomethingElse();
}

UPDATE >>>

No time left to help tonight... just some tips for you... look into the BooleanToVisibilityConverter online - with this, you can bind the Checkbox.IsChecked property directly to the Visibility property of one or more of your controls... no event handlers anywhere to be seen!

Also, you can just use a Grid to group your controls and set the Visibility property on that to make all of the controls visible or hidden at once. I hope that helps.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • No, it's simple labels, check boxes, and Textboxes. I would have posted a picture if I had 10+ rep XD – PRX Sep 19 '13 at 16:03
  • If this is *not* what you want, then please clearly explain *exactly* what you want in your question. – Sheridan Sep 19 '13 at 16:05
  • By toggling the checkbox, a textbox toggles visibility. If it's the checkbox for "Training" it also toggles the visiblity of the label and checkbox for "Evaluation". I don't know if there is a way to "link" these controls together and have a general method to do this for the Training and Evaluation of several things (other sets of checkboxes, labels, and textboxes). Or perhaps there is just a much better way to go about doing this (never done anything with MVVM before). – PRX Sep 19 '13 at 16:13
  • You are *not* doing anything with MVVM now. We don't handle events like this using MVVM. I suggest that you read up about it before you try it. – Sheridan Sep 19 '13 at 16:20
0

You need to bind the visibility of your controls to the checkbox's checked state. Without using a MVVM pattern or any backing properties in your code, the simplest implementation is as follows...

In your XAML, make sure you define the static resource (a converter) needed to make this work. You will need to add the namespace for wherever the converter C# code will exist.

<Window.Resources>
    <wpfApplication1:BoolToVisibilityConverter x:Key="BoolToVisibility" />
</Window.Resources>

The converter would be as follows, and is probably something every application should have, among other things.

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool && !(bool) value)
            return Visibility.Collapsed;
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

We need a converter because the visibility of the controls is represented by a enum value (Visibility.Whatever) instead of a boolean. We convert the boolean value of "IsChecked" into the appropriate enum value and set it.

Reference the converter in your UI to toggle visibility. The XAML would look like this.

<StackPanel>
   <CheckBox Name="ShowOrHide" Content="Toggle Me!" />
   <TextBox Visibility="{Binding IsChecked, 
                  ElementName=ShowOrHide,
                  Converter={StaticResource BoolToVisibility}}" />
</StackPanel>
Guttsy
  • 2,130
  • 1
  • 18
  • 29
  • Thanks. Had to also add Took a while to figure out how to get it to work because I was getting a metadata problem (because project was saved on network so moving it to local fixed it). Is there a way in the ways of Bindings to actively listen for these or would I just have to create my own listeners? – PRX Sep 19 '13 at 21:00
  • I am not certain what you mean by actively listening "for these." Can you elaborate? Sorry if I'm missing something obvious. – Guttsy Sep 19 '13 at 22:33
  • Nevermind, in all the effort to get the logic setup I forgot to set the checkbox to equal to equivalent database object value haha – PRX Sep 20 '13 at 12:39