简体   繁体   中英

DataTemplate not used when WPF control is opened within Windows Form Control

I have a DataTemplate that I created to manage the display of the items in a ListBox. When I open the control in a regular WPF application, it works correctly. However, when I open it in aa Windows Form control hosted in a Windows Form application, the datatemplate is not used. Thinking that it may be a problem with "FindResource" in that context, I added the DataTemplate in the code-behind. Once again it worked correctly when opening in a regular WPF app, but failed when opening in the Windows Form app.

If I set the background of the ListBox itself, I see the background as well as blank rows for all of the items that "should" be displayed - so I know the data is getting there, its just that the template does not apply itself.

code to load windows form control:

_elementHost = new ElementHost();
_elementHost.Dock = DockStyle.Fill;
this.Controls.Add(_elementHost);
NavigationControl userControl = new NavigationControl(); // the wpf control
_elementHost.Child = userControl;

dataTemplate xaml:

<DataTemplate x:Key="WorkingAccountResultTemplate" >
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Test" Width="50" Foreground="Purple" Background="AliceBlue"/>
        <TextBlock Text="{Binding ItemKeyId}" HorizontalAlignment="Stretch" Background="Maroon" />
    </StackPanel>
</DataTemplate>

You need to start the WPF instance in your application. In order to do this create an App.xaml file in your startup or main winforms project.

App.xaml, you should have the following lines, in addition to the includes and class declaration: (You can have an empty file with just the includes, so this section can be empty)

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
            </ResourceDictionary.MergedDictionaries>
</Application.Resources>

App.xaml, In the code behind:

public partial class App : Application
{
    public App()
    {
        StyleManager.ApplicationTheme =new Windows7Theme();
        InitializeComponent();
    }

    public static void EnsureApplicationResources()
    {
        if (Application.Current == null)
        {
            // create the Application object
            new App {ShutdownMode = ShutdownMode.OnExplicitShutdown};                                
        }
    }

    protected override void OnExit(ExitEventArgs e)
    {
        if(Current != null)
            Current.Shutdown();

        base.OnExit(e);
    }
}

In the start-up method in Main.cs or Program.cs:

private static void Main()
{
// Your initialization code

//WPF instance start
                App.EnsureApplicationResources();

                Application.Run(MainForm.Instance);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM