Question: I need to use a WebView to load certain webpages and dynamically change the css before showing them to the user (which means I have to delete all `<link>` tags and append the one with my css). (Why? Because I want to adapt the look of a particular site - which is not mine - for smartphones)

Now, I've seen that similar questions have been answered that the only way to modify the html before showing it to the user is by executing some javascript in the onPageFinished method; this could be a solution, but I'd like to consider other possibilities as well.

So, my questions are: 

1) If I go deeper in the source of the WebView class, is it possible to find where the html is loaded from the site, so that I have direct access to the html and I can modify it as I want?

2) If yes, is WebView the class that handles the communication and retrieves the html? If else, which one is it?

3) Assuming that what I asked is possible, do you think that the application would perform better if the modification to the html where made this way instead of using javascript?

Answer: 
**EDIT:** I wrote this post as an inexperienced WPF developer, nowadays I'd either use a framework that provides event to command binding, or simply use a button and restyle it. Of course for maximum flexibility this is maybe better.

I find the best way to do this is to create a simple user control wrapper for my content, with dependency properties for the command and parameter.

The reason I did this was due to the Button not bubbling the click event to my ListBox which prevented it from selecting the ListBoxItem.

CommandControl.xaml.cs:

```
public partial class CommandControl : UserControl
{
    public CommandControl()
    {
        MouseLeftButtonDown += OnMouseLeftButtonDown;
        InitializeComponent();
    }

    private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
    {
        if (Command != null)
        {
            if (Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter);
            }
        }
    }

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand),
            typeof(CommandControl),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None));

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object),
            typeof(CommandControl),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None));

    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }
}

```

CommandControl.xaml:

```
<UserControl x:Class="WpfApp.UserControls.CommandControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         Background="Transparent">
</UserControl>

```

Usage:

```
<ListBoxItem>
    <uc:CommandControl Command="{Binding LoadPageCommand}"
                       CommandParameter="{Binding HomePageViewModel}">
        <TextBlock Text="Home" Margin="0,0,0,5" VerticalAlignment="Center"
                   Foreground="White" FontSize="24" />
    </uc:CommandControl>
</ListBoxItem>

```

The Content can be whatever, and when the control is clicked, it will execute the command.

**EDIT:** Added `Background="Transparent"` to UserControl to enable click events on the entire area of the control.
I recently needed to trigger an `ICommand` upon double clicking a `ListBoxItem` as well.

Personally, I don't like the `DataTemplate` method as it is binding to the content inside the `ListBoxItem` container, and not the container itself. I've opted to use an Attached Property to assign an `InputBinding` on the container. It takes a little more elbow grease, but it works well.

First, we need to create an attached property class. I've created mine a little more generically towards any class that derives from `FrameworkElement`, just in case I run into this again with a different visual.

```
public class FrameworkElementAttachedProperties : DependencyObject
{
    public static readonly DependencyProperty DoubleClickProperty = DependencyProperty.RegisterAttached("DoubleClick", typeof(InputBinding),
        typeof(FrameworkElementAttachedProperties), new PropertyMetadata(null, OnDoubleClickChanged));

    public static void SetDoubleClick(FrameworkElement element, InputBinding value)
    {
        element.SetValue(DoubleClickProperty, value);
    }

    public static InputBinding GetDoubleClick(FrameworkElement element)
    {
        return (InputBinding)element.GetValue(DoubleClickProperty);
    }

    private static void OnDoubleClickChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = obj as FrameworkElement;

        /// Potentially throw an exception if an object is not a FrameworkElement (is null).

        if(e.NewValue != null)
        {
            element.InputBindings.Add(e.NewValue as InputBinding);
        }
        if(e.OldValue != null)
        {
            element.InputBindings.Remove(e.OldValue as InputBinding);
        }
    }
}

```

Then the final step is to override the base container style for the `ListBoxItem`.

```
<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}"
        BasedOn="{StaticResource ListBoxItem}">
        <Setter Property="local:FrameworkElementAttachedProperties.DoubleClick">
            <Setter.Value>
                <MouseBinding Command="{Binding OnListBoxItemDoubleClickCommand}"
                    MouseAction="LeftDoubleClick"/>
            </Setter.Value>
        </Setter>
    </Style>
</ListBox.ItemContainerStyle>

```

Now, anytime a `ListBoxItem` is double clicked, it will fire our `OnListBoxItemDoubleClickCommand`.