[英]Binding WindowsFormsHost Child property
我正在創建MVVM應用程序。
在我的模型中,我需要處理 View中顯示的System.Windows.Forms.Panel 。 我的想法是在ViewModel中 創建此Panel,然后從一側將View綁定到它 ,在另一側,將其句柄傳遞給模型 。
我有一個WindowsFormsHost控件:
<Page x:Class="Test.Views.RenderPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Test.Views"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="1200"
Title="Page1">
<DockPanel>
<WindowsFormsHost x:Name="winformsHost" Child="{Binding RenderPanel}"/>
</DockPanel>
</Page>
我想將其Child屬性與ViewModel提供的RenderPanel綁定
public ObservableObject<System.Windows.Forms.Panel> RenderPanel { get; private set; }
public VideoRecorderViewModel ()
{
RenderPanel = new System.Windows.Forms.Panel (); //Bind it here
var model = new Model (RenderPanel.Handle); pass it to the model
}
但是,我得到一個錯誤,說:
System.Windows.Markup.XamlParseException:
A 'Binding' cannot be set on the 'Child' property of type 'WindowsFormsHost'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
如何解決這個問題?
該錯誤不言自明。 您不能綁定到屬於WPF WindowsFormsHost控件的子對象的對象的屬性
您可以創建一個帶有一些附加屬性的類來實現此目的: 是否無法綁定到屬於C#/ XAML應用程序中WindowsFormsHost Child對象的屬性的解決方法?
或者,您也可以使用ContentControl對其進行包裝,如下所示: 創建了Bindable WindowsFormsHost,但子更新未反映到控件中
創建WindowsFormHost類型的依賴項屬性和對象,並在屬性Chaged事件處理程序中設置子屬性。
using System.Windows.Forms.Integration;
namespace MainStartUp.DependencyObjects
{
public class FormHostDependencyObject : WindowsFormsHost
{
public static readonly DependencyProperty ContentControlProperty =
DependencyProperty.Register("ContentControl",
typeof(System.Windows.Forms.Control),
typeof(FormHostDependencyObject),
new PropertyMetadata(new System.Windows.Forms.Control(),
PropertyChaged));
public static void SetContentControl(UIElement element, string value)
{
element.SetValue(ContentControlProperty, value);
}
public static string GetContentControl(UIElement element)
{
return (string)element.GetValue(ContentControlProperty);
}
private static void PropertyChaged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
((FormHostDependencyObject)dependencyObject).Child =
(System.Windows.Forms.Control)e.NewValue;
}
}
}
如下在xaml中使用它。
<UserControl x:Class="MainStartUp.Views.WIndowsHostUserControl"
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"
xmlns:local="clr-namespace:MainStartUp.Views"
xmlns:DependecyObjects="clr-
namespace:MainStartUp.DependencyObjects"
xmlns:wf="clr-
namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<!--<ContentControl Content="{Binding ContentControl}" />-->
<DependecyObjects:FormHostDependencyObject ContentControl="{Binding
ContentControl}"></DependecyObjects:FormHostDependencyObject>
</Grid>
</UserControl>
在我的視圖模型中,我有一個對象屬性,將為其托管窗口控件對象:
public object ContentControl
{
get
{
return _contentControl;
}
set
{
_contentControl = value;
RaisePropertyChangedEvent("ContentControl");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.