Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Silverlight 4 toolkit sample of AutoCompleteBox, how to toggle drop down in xaml?

Sample taken straight from the Silverlight 4 Toolkit - Samples source code.

We have a style for the AutoCompleteBox to make it like a combobox :

<ControlTemplate TargetType="input:AutoCompleteBox">
              <Grid Margin="{TemplateBinding Padding}">
                ...
                Click="DropDownToggle_Click">

Now, in their sample, they have a click event handler in code behind (listed below), however I was trying to define this method in the xaml ( i.e. I don't want a code behind file )

private void DropDownToggle_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement fe = sender as FrameworkElement;
            AutoCompleteBox acb = null;
            while (fe != null && acb == null)
            {
                fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
                acb = fe as AutoCompleteBox;
            }
            if (acb != null)
            {
                if (string.IsNullOrEmpty(acb.SearchText))
                {
                    acb.Text = string.Empty;
                }
                acb.IsDropDownOpen = !acb.IsDropDownOpen;
            }
        }

Is this possible ?

like image 410
RyBolt Avatar asked Dec 06 '25 13:12

RyBolt


1 Answers

I have replaced the whole line (starting with Click=...), with the following;

IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}" 

I have eliminated the need now for the event handler method.

like image 128
RyBolt Avatar answered Dec 09 '25 12:12

RyBolt