Search This Blog

Showing posts with label Controls. Show all posts
Showing posts with label Controls. Show all posts

Monday, November 4, 2013

Control Templates WPF

WPF > Controls  > Templates

Windows Presentation Foundation Control Templates allows developers to completely change the look and feel of the controls.

Almost every control in WPF uses a control template to define how it displays and its contents. You can override the control template on  built-in control to make it appear as you want.

ControlTemplate WPF Example

WPF > Controls  > Templates > ControlTemplate

ControlTemplate defines the visual elements tree that is applied as the control template for a control.
ContentPresenter it is a placeholder for any XAML content and it can be used to insert content at runtime.

Example: Create Ellipse Button

<Window x:Class="WPF_Tutorial.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">

    <Window.Resources>
        <ControlTemplate TargetType="Button" x:Key="ctButton" >
            <Grid>
                <Ellipse Fill="{ TemplateBinding Background }" Height="{TemplateBinding Height }">
                </Ellipse>
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Button.Content}" />
            </Grid>
        </ControlTemplate>
    </Window.Resources>

    <Grid>
        <Button Content="Ellipse Button" HorizontalAlignment="Left" VerticalAlignment="Top"
                Background="BlueViolet"
                Width="100" Height="100" Template="{ StaticResource ctButton}" Foreground="#FFF3E2E2">
        </Button>
    </Grid>
</Window>

Friday, November 1, 2013

Disable Script Errors WebBrowser WPF

WPF > Controls  > WebBrowser > Disable Script Errors

WebBrowser WPF control doesn't have property to disable script errors.
Using reflection we can solve this problem.

Solution:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   wb1.Navigated += new NavigatedEventHandler(WebBrowser_Navigated);
}

void WebBrowser_Navigated(object sender, NavigationEventArgs e)
{
   HideJsScriptErrors((WebBrowser) sender);
}

public void HideJsScriptErrors(WebBrowser wb)
{
   // IWebBrowser2 interface
   // Exposes methods that are implemented by the WebBrowser control 
   // Searches for the specified field, using the specified binding constraints.
   FieldInfo fld = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
   if (fld == null)
       return;
   object obj = fld.GetValue(wb);
   if (obj == null)
       return;
// Silent: Sets or gets a value that indicates whether the object can display dialog boxes.
// HRESULT IWebBrowser2::get_Silent(VARIANT_BOOL *pbSilent);HRESULT IWebBrowser2::put_Silent(VARIANT_BOOL bSilent);
     obj.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, obj, new object[] { true });
}

Wednesday, October 30, 2013

Windows Presentation Foundation (WPF) Controls

WPF > Controls

A control is any class that represents a visible object in an application.
WPF comes with a lot of UI components such as Button, Label, TextBox, ListBox, etc.
In WPF a class does not need to inherit from the Control class to have a visible presence.
ControlTemplate  allows a consumer of a control to change the control's appearance without having to create a new subclass.