【问题标题】:WindowsFormsHost and DependencyPropertyWindowsFormsHost 和 DependencyProperty
【发布时间】:2010-04-22 19:24:18
【问题描述】:

我有一个 Windows 窗体控件,我正在尝试使用 WindowsFormsHost 类将其包装为 WPF 控件;我想将旧控件绑定到视图模型。具体来说,该控件公开了一个网格属性 GridVisible,我想将视图模型绑定到该属性。我使用一个私有的、静态的支持字段和一个静态的、只读的属性来表示依赖属性(在功能上与静态的公共字段相同,但更少混乱)。当我尝试通过 XAML 设置控件的 GridVisible 属性时,它没有更新。想法?我做错了什么?

绘图主机类

/// <summary>
/// Provides encapsulation of a drawing control.
/// </summary>
public class DrawingHost : WindowsFormsHost
{
    #region Data Members

    /// <summary>
    /// Holds the disposal flag.
    /// </summary>
    private bool disposed;

    /// <summary>
    /// Holds the grid visible property.
    /// </summary>
    private static readonly DependencyProperty gridVisibleProperty =
        DependencyProperty.Register("GridVisible", typeof(bool),
        typeof(DrawingHost), new FrameworkPropertyMetadata(false,
            FrameworkPropertyMetadataOptions.AffectsRender |
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    /// <summary>
    /// Holds the pad.
    /// </summary>
    private readonly DrawingPad pad = new DrawingPad();

    #endregion

    #region Properties

    /// <summary>
    /// Get or set whether the grid is visible.
    /// </summary>
    public bool GridVisible
    {
        get { return (bool)GetValue(GridVisibleProperty); }
        set { SetValue(GridVisibleProperty, pad.GridVisible = value); }
    }

    /// <summary>
    /// Get the grid visible property.
    /// </summary>
    public static DependencyProperty GridVisibleProperty
    {
        get { return gridVisibleProperty; }
    }

    #endregion

    /// <summary>
    /// Default-construct a drawing host.
    /// </summary>
    public DrawingHost()
    {
        this.Child = this.pad;
    }

    /// <summary>
    /// Dispose of the drawing host.
    /// </summary>
    /// <param name="disposing">The disposal invocation flag.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && !disposed)
        {
            if (pad != null)
            {
                pad.Dispose();
            }
            disposed = true;
        }
        base.Dispose(disposing);
    }
}


XAML

<UserControl x:Class="Drawing.DrawingView"
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" 
mc:Ignorable="d" 
xmlns:local="clr-namespace:Drawing">
<local:DrawingHost GridVisible="True"/></UserControl>

【问题讨论】:

    标签: wpf binding prism windowsformshost dependency-properties


    【解决方案1】:

    依赖属性的首要规则之一是永远不要在 get 和 set 中包含任何逻辑,除了 GetValueSetValue 调用。这是因为在 XAML 中使用它们时,它们实际上并不通过 get 和 set 访问器。它们与GetValueSetValue 调用内联。所以你的任何代码都不会被执行。

    执行此操作的适当方法是使用DependencyProperty.Register 方法中的PropertyMetadata 参数设置回调。然后在回调中你可以执行任何额外的代码。

    【讨论】:

    • 谢谢,查理!我指定了一个赋值回调,它按预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多