New User?   Join Now     Login            Forgot Password?    
XamlQuery v1.2
Examples and Demos
Posted on Aug-08-2010 (6983 hits)

Examples of XamlQuery

The following are some examples on the usage of XamlQuery. These examples illustrate that XamlQuery can help simplify complex tasks for developers.

//hide all lines (method-1)
XamlQuery.ByType<Line>(MainCanvas).Hide();

//hide all lines (method-2)
XamlQuery.Search(MainCanvas, "Line").Hide();

//disable all textboxes (method-1)
XamlQuery.ByType<TextBox>(RegisterGrid).SetValue(Control.IsEnabledProperty, false);

//disable all textboxes (method-2)
XamlQuery.Search(RegisterGrid, "TextBox").SetValue(Control.IsEnabledProperty, false);

//remove border of all textboxes
XamlQuery.ByType<TextBox>(RegisterGrid).SetValue(Control.BorderBrushProperty, new SolidColorBrush(Colors.Transparent));

//set font-style of all textboxes
XamlQuery.ByType<TextBox>(RegisterGrid).SetValue(Control.FontStyleProperty, FontStyles.Italic);

//change IsTabStop and Foreground of all hyperlinks
XamlQuery.ByType<HyperlinkButton>(LayoutRoot).SetValue(Control.IsTabStopProperty, false);
XamlQuery.ByType<HyperlinkButton>(LayoutRoot).SetValue(Control.ForegroundProperty, new SolidColorBrush(Colors.Black));

//find all controls with name "ArrowLine" (method-1)
ControlSet allArrowLines = XamlQuery.ByProperty(MainCanvas, FrameworkElement.NameProperty, "ArrowLine");

//find all controls with name "ArrowLine" (method-2)
ControlSet allArrowLines = XamlQuery.Search(MainCanvas, "*[FrameworkElement_Name=ArrowLine]");

Demonstration of XamlQuery using a Canvas

The following demo illustrates some animation and style related tasks using a Canvas control.

The event-handler of the buttons in this demo are shown below.

private void ChangeStyleButton_Click(object sender, RoutedEventArgs e)
{
    //find all rectangles inside "MainCanvas"
    ControlSet allShapes = XamlQuery.ByType<Rectangle>(MainCanvas);
    
    //set style for all rectangles
    allShapes.SetValue(Shape.StrokeThicknessProperty, 4.0);
    allShapes.SetValue(Shape.FillProperty, new SolidColorBrush(Colors.LightGray));
    allShapes.SetValue(Rectangle.RadiusXProperty, 8.0);
    allShapes.SetValue(Rectangle.RadiusYProperty, 8.0);
    
    //set mouse-enter event for all rectangles
    allShapes.Bind(EventType.MouseEnter, new MouseEventHandler(delegate(object senderObject, MouseEventArgs eventArgs)
    {
        Rectangle rectangle = (Rectangle)senderObject;
        rectangle.SetValue(Shape.FillProperty, new SolidColorBrush(Colors.Green));
    }));
}

private void ToggleFadeButton_Click(object sender, RoutedEventArgs e)
{
    XamlQuery.ByType<Rectangle>(MainCanvas).FadeToggle(Speed.Fast);
}

private void ToggleSlideButton_Click(object sender, RoutedEventArgs e)
{
    XamlQuery.ByType<Rectangle>(MainCanvas).SlideToggle(Speed.Fast);
}

private void ClearButton_Click(object sender, RoutedEventArgs e)
{
    XamlQuery.All(MainCanvas).Detach();
}

Demonstration of XamlQuery using a simple Form

The following demo illustrates the usage of XamlQuery using a simple registration form.

The event-handler of the buttons in this demo are shown below.

private void RegisterButton_Click(object sender, RoutedEventArgs e)
{
    //validate controls marked compulsory (faster version)
    bool allValid = true;
    XamlQuery.ByType<Control>(LayoutRoot).FilterByProperty(FrameworkElement.TagProperty, "Compulsory").ForEach(delegate(DependencyObject obj)
    {
        if (ControlSet.Create(obj).Val().Equals(string.Empty))
        {
            MessageBox.Show("Enter Value for " + ((Control)obj).Name);
            allValid = false;
            return;
        }
    });

    if (allValid)
    {
        MessageBox.Show("You are ready to be registered.");
    }
}

private void ChangeStyleButton_Click(object sender, RoutedEventArgs e)
{
    ControlSet inputControls = XamlQuery.ByType<Control>(LayoutRoot);
    inputControls.RemoveByType<Button>();
    inputControls.SetValue(Control.BorderBrushProperty, new SolidColorBrush(Colors.Green));
    inputControls.SetValue(Control.BackgroundProperty, new SolidColorBrush(Colors.LightGray));
    inputControls.SetValue(Control.FontWeightProperty, FontWeights.SemiBold);
    inputControls.SetValue(Control.FontStyleProperty, FontStyles.Italic);
}

In this example, the input controls (like TextBox, ComboBox) whose input is mandatory are marked as "Compulsory" using Tag property.


About      Terms      Contact