Jason
Jason

Reputation: 343

show/hide controls based on dropdown list value

I have an application that I am working on, and need to show/hide controls based on a selected value in a dropdown list. By default most of the controls will be hidden and when a value ("VFD") is selected from the Control Device control, the hidden controls will become visible.

I have an entity name MCCLoads, which has relationships with all of the other entities that drive the dropdown lists.

The screen that i want to apply this functionality to is named MCCLoadsSetListDetail

I was trying to use this bit of code, but I'm not sure if this is the proper way to go

public void MCCLoadsSetListDetail_SelectionChanged()
    {
        this.FindControl("CTRL_DEVICE").IsEnabled = true;

        if (this.MCCLoadsSetListDetail.SelectedItem.Loads_CTRL_Device == "VFD")
        {
            this.FindControl("Line_Reactor_IMP").IsVisible = false;
        }
    }

MCC Loads Form MCC Loads Table with relationships

Thanks in advance, Jason

Upvotes: 3

Views: 3274

Answers (1)

Alex Hinton
Alex Hinton

Reputation: 1618

Inside the Created method for your screen subscribe to the ControlAvailable event. When this is fired you can cast your Silverlight control to a Windows control to subscribe to the SelectionChanged event. You can then respond to this as you wish...

partial void YourScreen_Created()
{
    this.FindControl("YourDropDown").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(YourDropDown_ControlAvailable);
}

void YourDropDown_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
    var ctrl = e.Control as AutoCompleteBox;
    ctrl.SelectionChanged += new SelectionChangedEventHandler(ctrl_SelectionChanged);
}

void ctrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    dynamic selectedItem = (sender as AutoCompleteBox).SelectedItem;
    if (selectedItem == null)
    {
        return;
    }

    // Your logic goes here....
    this.FindControl("CTRL_DEVICE").IsEnabled = true;
    if (selectedItem.StringValue == "VFD")
    {
        this.FindControl("Line_Reactor_IMP").IsVisible = false;
    }
}

Hope this helps...

Upvotes: 3

Related Questions