简体   繁体   English

SharpGL + WPF:Draw是从未调用的事件

[英]SharpGL + WPF: Draw are events never called

I'm trying to build a SharpGL app with WPF, but have trouble integrating a view model. 我正在尝试使用WPF构建SharpGL应用,但无法集成视图模型。

I bind the DataContext of an OpenGLControl to an OpenGLControl property in my ViewModel and create events for the drawing functions in the view model, but they never get called. 我将OpenGLControlDataContext绑定 ViewModel中 的OpenGLControl属性,并在视图模型中为绘图函数创建事件,但从未调用它们。

The OpenGLControl just appears as a black screen. OpenGLControl只是显示为黑屏。 When I just implement the drawing function in a code behind xaml.cs file it works, but I really want to use a viewModel. 当我只是在xaml.cs文件后面的代码中实现绘图功能时,它可以工作,但是我真的想使用viewModel。

<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>

[...] [...]

<StackPanel Orientation="Horizontal">
        <GroupBox Width="80" Header="Controls">
            <StackPanel>
                <TextBox Text="{Binding TranslationX}" />
                <TextBox Text="{Binding TranslationY}" />
                <TextBox Text="{Binding TranslationZ}" />
            </StackPanel>

        </GroupBox>
        <sharpGL:OpenGLControl x:Name="GLControl" DataContext="{Binding OpenGLControl}" MinWidth="350"/>
</StackPanel>

ViewModel code: ViewModel代码:

    private OpenGLControl openGLControl = new OpenGLControl();
    public OpenGLControl OpenGLControl
    {
        get
        {
            return openGLControl;
        }
        set
        {
            openGLControl = value;
            NotifyPropertyChanged(); //Custom implementation of 
                                     //INotifyPropertyChanged
        }
    }
    public ViewModel()
    {
        OpenGLControl.OpenGLDraw += drawEvent;
    }
    private void drawEvent(object sender, OpenGLEventArgs args)
    {
        draw(args.OpenGL); // draws a number of vertices, works when used in 
                           //  code behind
    }

A DataContext in WPF controls how and to what exact Properties a Binding will bind to. WPF中的DataContext控制绑定将绑定的方式以及绑定到的确切属性。 What you are doing by setting a DataContext like <sharpGL:OpenGLControl DataContext="{Binding OpenGLControl}" /> is essentially this: When searching for properties required by a Binding , search inside the OpenGLControl ViewModel. 通过设置诸如<sharpGL:OpenGLControl DataContext="{Binding OpenGLControl}" />本质上是这样的:当搜索Binding所需的属性 ,请在OpenGLControl ViewModel中进行搜索。

This does not work because your are creating two OpenGLControl's one which is created in the XAML via <sharpGL:OpenGLControl/> and a second one which is created inside your ViewModel. 这不起作用,因为您正在创建两个OpenGLControl,一个是通过<sharpGL:OpenGLControl/>在XAML中创建的,另一个是在ViewModel内部创建的。 Next you set events for your ViewModel (which is not visible) and instruct the OpenGLControl created in the XAML that it should search for data that is required by any Binding to look inside the OpenGLControl in the ViewModel. 接下来,为您的ViewModel设置事件(不可见),并指示在XAML中创建的OpenGLControl它应搜索任何绑定所需的数据以在ViewModel的OpenGLControl内部查找。

Since OpenGLControl is a Control, you should probably not think of it as a good canditate for a ViewModel. 由于OpenGLControl是一个控件,因此您可能不应该将其视为ViewModel的合适候选人。 Instead try to create an eventhandler in the window and forward all draw event callbacks from the window to your ViewModel by eg calling a delegate. 而是尝试在窗口中创建事件处理程序,并通过例如调用委托将所有绘制事件回调从窗口转发至ViewModel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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