Using WindowsFormsHost to host a Winforms UserControl in a RadTabItem

2 posts, 0 answers
  1. Robs
    Robs avatar
    3 posts
    Member since:
    Sep 2011

    Posted 16 Sep 2011 Link to this post

    Hi,

    I currently trying to convert a Winforms Project to WPF Project. As step one I need to create a Tabbed MDI container in WPF in which each new RadItemTab will contain a WindowsFormHost which will again contain the existing WinForm UserControl.

    We got the RadControl to work up to our expectation but unable to include the WindowsFormHost.

    I'm pasting the sample of what i was trying out would really appreciate your help on this.

    Download
    Full Project

    Or

    AnotherTab.xaml
    <UserControl x:Class="FcWpfTabControl.AnotherTab"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 xmlns:xx="clr-namespace:FcWpfTabControl"
                 mc:Ignorable="d"
                 d:DesignHeight="300" d:DesignWidth="300">
        <UserControl.Resources>
            <Style x:Key="CloseButton" TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Trigger.EnterActions>
                                        <BeginStoryboard x:Name="MouseOverBeginStoryboard">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames
                                                        Storyboard.TargetProperty="(UIElement.Visibility)"
                                                        Storyboard.TargetName="FocusEllipse">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Visible</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                                <ColorAnimation Duration="0" To="LightGray"
                                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                        Storyboard.TargetName="FocusEllipse" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </Trigger.EnterActions>
                                    <Trigger.ExitActions>
                                        <StopStoryboard BeginStoryboardName="MouseOverBeginStoryboard" />
                                    </Trigger.ExitActions>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <Trigger.EnterActions>
                                        <BeginStoryboard x:Name="IsPressedBeginStoryboard">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames
                                                        Storyboard.TargetProperty="(UIElement.Visibility)"
                                                        Storyboard.TargetName="FocusEllipse">
                                                    <DiscreteObjectKeyFrame KeyTime="0">
                                                        <DiscreteObjectKeyFrame.Value>
                                                            <Visibility>Visible</Visibility>
                                                        </DiscreteObjectKeyFrame.Value>
                                                    </DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                                <ColorAnimation Duration="0" To="DarkGray"
                                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                        Storyboard.TargetName="FocusEllipse" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </Trigger.EnterActions>
                                    <Trigger.ExitActions>
                                        <StopStoryboard BeginStoryboardName="IsPressedBeginStoryboard" />
                                    </Trigger.ExitActions>
                                </Trigger>
                            </ControlTemplate.Triggers>
                            <Grid Background="Transparent" Width="14" Height="14">
                                <Ellipse x:Name="FocusEllipse" Fill="#FFF13535" Visibility="Collapsed" />
                                <ContentPresenter x:Name="ContentPresenter"
                                        Content="{TemplateBinding Content}" HorizontalAlignment="Center"
                                        VerticalAlignment="Center" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="ClosableStyle" TargetType="telerik:RadTabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding Title}" />
                                    <telerik:RadButton Grid.Column="1" Style="{StaticResource CloseButton}"
                                                    Width="16" Height="16"
                                        Margin="10,0,0,0" ToolTipService.ToolTip="Remove item"
                                                  xx:RoutedEventHelper.EnableRoutedClick="True"
                                                  Padding="0" >
                                    <ContentControl>
                                            <Path Data="M0,0 L6,6 M6, 0 L0,6" Stroke="Black" StrokeThickness="1"
                                    SnapsToDevicePixels="True" />
                                    </ContentControl>
                                </telerik:RadButton>
                      
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <!--<ContentControl Content="{Binding Content}"/>-->
                        <DockPanel Name="panel1" LastChildFill="True">
                            <WindowsFormsHost Name="wfHost" DockPanel.Dock="Left" Child="{Binding Content}"/>
                        </DockPanel>
                    </Setter.Value>
                </Setter>
            </Style>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <telerik:RadTabControl x:Name="tabControl" ItemContainerStyle="{StaticResource ClosableStyle}">
            </telerik:RadTabControl>
        </Grid>
    </UserControl>

    AnotherTab.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Telerik.Windows.Controls;
    using System.Collections.ObjectModel;
    using System.Windows.Forms.Integration;
    using System.Windows.Forms;
     
    namespace FcWpfTabControl
    {
        /// <summary>
        /// Interaction logic for AnotherTab.xaml
        /// </summary>
        public partial class AnotherTab : System.Windows.Controls.UserControl
        {
            public AnotherTab()
            {
                InitializeComponent();
                 
                EventManager.RegisterClassHandler(typeof(RadTabItem), RoutedEventHelper.CloseTabEvent, new RoutedEventHandler(OnCloseClicked));
     
            }
            ObservableCollection<TabItemModel> tabItemsModel = new ObservableCollection<TabItemModel>();
     
            public void OnCloseClicked(object sender, RoutedEventArgs e)
            {
                var tabItem = sender as RadTabItem;
                // Remove the item from the collection the control is bound to
                tabItemsModel.Remove(tabItem.DataContext as TabItemModel);
            }
            public void CreateTabItem(UserControl uc)
            {
                // Create items:
     
                RadTabItem Item1 = new RadTabItem();
                WindowsFormsHost wfh = new WindowsFormsHost();
                wfh.Child = uc;
                Item1.Content = wfh;
                Item1.Header = "Tab1";
     
                TabItemModel tb = new TabItemModel("Item", uc);
               // tb.userControl = uc;
                tabItemsModel.Add(tb);
               // tabItemsModel[0].Content.Child = uc;
     
                tabControl.ItemsSource = tabItemsModel;
     
     
            }
        }
     
        public class TabItemModel
        {
            
            public TabItemModel(string stringTitle, UserControl uc)
            {
                WindowsFormsHost wfh = new WindowsFormsHost();
               //wfh.Child = uc;
                Title = stringTitle;
                Content = wfh;
                 
            }
            public String Title
            {
                get;
                set;
            }
     
            public UserControl userControl
            {
                get;
                set;
            }
     
            public WindowsFormsHost Content
            {
                get;
                set;
            }
        }
    }

    RoutedEventHelper.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Telerik.Windows.Controls;
    using System.Windows;
    using System.Windows.Controls;
    using Telerik.Windows;
     
     
    namespace FcWpfTabControl
    {
        public class RoutedEventHelper
        {
            //Create the routed event:
            public static readonly RoutedEvent CloseTabEvent = EventManager.RegisterRoutedEvent(
                "CloseTab",
                RoutingStrategy.Bubble,
                typeof(RoutedEventHandler),
                typeof(RoutedEventHelper));
            //Add an attached property:
            public static bool GetEnableRoutedClick(DependencyObject obj)
            {
                return (bool)obj.GetValue(EnableRoutedClickProperty);
            }
            public static void SetEnableRoutedClick(DependencyObject obj, bool value)
            {
                obj.SetValue(EnableRoutedClickProperty, value);
            }
            // Using a DependencyProperty as the backing store for EnableRoutedClick.
            // This enables animation, styling, binding, etc...
            public static readonly DependencyProperty EnableRoutedClickProperty = DependencyProperty.RegisterAttached(
                "EnableRoutedClick",
                typeof(bool),
                typeof(RoutedEventHelper),
                new System.Windows.PropertyMetadata(OnEnableRoutedClickChanged));
            private static void OnEnableRoutedClickChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                var newValue = (bool)e.NewValue;
                var button = sender as Button;
                if (button == null)
                    return;
                if (newValue)
                    button.Click += new RoutedEventHandler(OnButtonClick);
            }
            static void OnButtonClick(object sender, RoutedEventArgs e)
            {
                var control = sender as Control;
                if (control != null)
                {
                    control.RaiseEvent(new RadRoutedEventArgs(RoutedEventHelper.CloseTabEvent, control));
                }
            }
        }
    }
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 21 Sep 2011 Link to this post

    Hello Robs,

     Unfortunately ,we were unable to run your project . Is it possible for you to send us a runnable sample that shows the issue so that we could investigate it better ? Thank you for your cooperation in advance.

    Best wishes,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top