【问题标题】:Question about using the WindowsFormsHost. (with DataBinding)关于使用 WindowsFormsHost 的问题。 (使用数据绑定)
【发布时间】:2019-12-13 13:22:37
【问题描述】:

我的英语水平很差,因为我不是以英语为母语的人。 希望你能理解。

我需要使用 WindowsFormsHost 控件,因为使用的是 WinForm 的 DataGridView。 由于某些原因,我无法在 WPF 中进行 DataGrid 控件。

在 .cs 文件中,我成功地将 WindowsFormsHost 与 WinForm 的 DataGridView 一起使用。 代码如下所示。

var tabItem = new ClosableTab();


#region Core Logic
var winformControl = new WindowsFormsHost();
winformControl.VerticalAlignment = VerticalAlignment.Stretch;
winformControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

winformControl.Child = new DataGridView();
DataGridView parsingTableView = winformControl.Child as DataGridView;
parsingTableView.EditMode = DataGridViewEditMode.EditProgrammatically;
parsingTableView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
parsingTableView.DataSource = mainWindow.editor.TokenTable;
parsingTableView.CellMouseEnter += new DataGridViewCellEventHandler(this.tableGridView_CellMouseEnter);

tabItem.Content = winformControl;
#endregion


this.mainWindow.tablControl.Items.Add(tabItem);

现在我想把上面的逻辑转换成XAML,所以我写了如下所示的逻辑。

首先,我定义了 DataTemplate 来显示 DataTable 类型。

//在资源文件中

xmlns:systemData="clr-namespace:System.Data;assembly=System.Data"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

<DataTemplate DataType="{x:Type systemData:DataTable}">
    <WindowsFormsHost>
        <WindowsFormsHost.Child>
            <wf:DataGridView DataSource="{Binding}" EditMode="EditProgrammatically" AutoSizeColumnsMode="AllCells"/>
        </WindowsFormsHost.Child>
    </WindowsFormsHost>
</DataTemplate>

现在在绑定到内容时使用上面的 DataTemplate,如下面的代码所示。

//在xaml文件中

<Grid>
    <TabControl ItemsSource="{Binding SelectedItem}">
        <TabItem Header="{lex:Loc Key=TokenTable}" Content="{Binding TokenTable}"/>
    </TabControl>
</Grid>

当我执行上述代码时,我在 DataSource="{Binding}" 处遇到如下错误。

如果我必须使用我糟糕的英语技能翻译上述错误。 错误告诉我。 “它不能在‘数据源’属性上配置‘绑定’。” "'Binding' 只能在 DependencyObject 的 DependencyProperty 上配置。"

我想我知道试图对我说什么错误,但我不知道如何解决上述问题。

我应该怎么做才能解决这个问题?

感谢您的阅读。

【问题讨论】:

    标签: c# wpf winforms windowsformshost


    【解决方案1】:

    不能直接绑定到DataGridViewDataSource 属性,因为它不是依赖属性。

    您可以做的是通过创建一个附加属性来解决此问题,该附加属性按照建议hereWindowsFormsHost 上设置,然后使用附加属性的回调设置DataGridView 的属性:

    public static class WindowsFormsHostMap
    {
        public static readonly DependencyProperty DataSourceProperty
            = DependencyProperty.RegisterAttached("DataSource", typeof(object), 
                typeof(WindowsFormsHostMap), new PropertyMetadata(OnPropertyChanged));
    
        public static string GetText(WindowsFormsHost element) => (string)element.GetValue(DataSourceProperty);
    
        public static void SetText(WindowsFormsHost element, object value) => element.SetValue(DataSourceProperty, value);
    
        static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var dataGridView = (sender as WindowsFormsHost).Child as DataGridView;
            dataGridView.DataSource = e.NewValue;
        }
    }
    

    XAML:

    <WindowsFormsHost local:WindowsFormsHostMap.DataSource="{Binding}">
    

    【讨论】:

    • 如果我听从您的建议,我想可以解决这个问题。谢谢你的回答。
    猜你喜欢
    • 2020-01-28
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多