Sajith Sageer
Sajith Sageer

Reputation: 173

WPF Expander should expand when a member control gets focus

I have two textboxes inside an expander. I am validating the textbox for text value when a button is clicked. If the textbox is empty the textbox is given foucs.

What I want to achieve is for the expander to expand automatically when one of these texboxes gets focus.

I could not find any ways to do that on the internet. Is it possible to do so?

xaml :

    <Grid>
        <Expander Header="Textbox Expander">
            <StackPanel Orientation="Vertical">
                <TextBox Name="txtName" Height="30" Width="100" Background="WhiteSmoke" />
                <TextBox Name="txtAge" Height="30" Width="100" Background="WhiteSmoke" />
            </StackPanel>
        </Expander>
        <Button Name="btnDone" Content="Done" Width="50" Height="50" Click="btnDone_Click"/>
    </Grid>

c# :

using System.Windows;
using System.Windows.Controls;

namespace TestExpanderFocus
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnDone_Click(object sender, RoutedEventArgs e)
        {
            Validation validation = new Validation();
            if(validation.validate(ref txtName) && validation.validate(ref txtAge))
            {
                //Do Something.
            }
        }
    }            
}

EDIT : Since this validation class is in another application I cannot edit this.

//Seperate class in another application which cannot be edited
public class Validation
{
    public bool validate(ref TextBox txtobj)
    {
        if (string.IsNullOrWhiteSpace(txtobj.Text))
        {
            MessageBox.Show("Please Enter Data..!");
            txtobj.Focus();
            return false;
        }
        return true;
    }
}

Upvotes: 0

Views: 549

Answers (1)

kurakura88
kurakura88

Reputation: 2305

What you wanted to achieve is actually pretty simple. First give the Expander a name

<Expander x:Name="MyExpander" ... />

Second, in your validation, before you focus on the textbox, simply expand the Expander

MyExpander.IsExpanded = true;
...

-- EDIT to satisy the new requirement --

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnDone_Click(object sender, RoutedEventArgs e)
    {
        Validation validation = new Validation();

        // since you know that the text will be focused when the validation fails
        var result1 = validation.validate(ref txtName);
        var result2 = validation.validate(ref txtAge);
        MyExpander.IsExpanded = !result1 || !result2;

        if(result1 && result2)
        {
           //Do Something.
        }
    }
}

But I must admit, this is not the nicest solution. There should be an easier way to just add Trigger to the Expander Style directly. (I will leave it to other people, since I don't have more time)

Upvotes: 1

Related Questions