C# WPF设计时属性

C# WPF设计时属性,c#,wpf,C#,Wpf,我构建了一个用户控件,它基本上是一个网格,可以在每一端有圆角,也可以是某个多边形。我有一个Rounded属性,可以更改圆角边框和要匹配的多边形的可见性(如果有人设置Rounded=“True”),则圆角边框可见,多边形隐藏,反之亦然 就像这个问题一样: …它在运行时工作得很好,但我似乎无法让它在设计时反映更改。但是,重新启动VS、清理解决方案、重建、更改构建目标等等,这些步骤似乎都没有什么不同。 我的课程很基础: public partial class MyBox : UserControl

我构建了一个用户控件,它基本上是一个网格,可以在每一端有圆角,也可以是某个多边形。我有一个Rounded属性,可以更改圆角边框和要匹配的多边形的可见性(如果有人设置Rounded=“True”),则圆角边框可见,多边形隐藏,反之亦然

就像这个问题一样:

…它在运行时工作得很好,但我似乎无法让它在设计时反映更改。但是,重新启动VS、清理解决方案、重建、更改构建目标等等,这些步骤似乎都没有什么不同。 我的课程很基础:

public partial class MyBox : UserControl
{
    public MyBox()
    {
        InitializeComponent();
    }

    public bool Rounded
    {
        get { return (bool)GetValue(RoundedProperty); }
        set
        {
            SetValue(RoundedProperty, value);
            this.edgeRounded.Visibility = (value ? Visibility.Visible : Visibility.Hidden);
            this.edgePolygon.Visibility = (value ? Visibility.Hidden : Visibility.Visible);
        }
    }

    public static readonly DependencyProperty RoundedProperty = DependencyProperty.Register("Rounded", typeof(bool), typeof(MyBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
}

有什么想法吗?

可以使用XAML将可见性属性绑定到datacontext viewmodel。
我已经用各种方法完成了这项工作。

也许可以使用XAML将visibility属性绑定到datacontext viewmodel。
我对各种事情都这样做。

我通过添加一个已更改的事件处理程序,然后更改依赖项属性注册以引用它来实现这一点。我的工作代码:

public partial class MyBox : UserControl
{
    public MyBox()
    {
        InitializeComponent();
    }

    public bool Rounded
    {
        get { return (bool)GetValue(RoundedProperty); }
        set { SetValue(RoundedProperty, value); }
    }

    public static readonly DependencyProperty RoundedProperty = DependencyProperty.Register("Rounded", typeof(bool), typeof(MyBox), new PropertyMetadata(false, RoundedChanged));

    private static void RoundedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        bool value = (bool)e.NewValue;
        MyBox thisMyBox = (MyBox)sender;

        // Hide/show the edges
        thisMyBox.edgeRounded.Visibility = (value ? Visibility.Visible : Visibility.Hidden);
        thisMyBox.edgePolygon.Visibility = (value ? Visibility.Hidden : Visibility.Visible);
    }
}

我通过添加一个已更改的事件处理程序,然后更改依赖项属性注册以引用它来实现这一点。我的工作代码:

public partial class MyBox : UserControl
{
    public MyBox()
    {
        InitializeComponent();
    }

    public bool Rounded
    {
        get { return (bool)GetValue(RoundedProperty); }
        set { SetValue(RoundedProperty, value); }
    }

    public static readonly DependencyProperty RoundedProperty = DependencyProperty.Register("Rounded", typeof(bool), typeof(MyBox), new PropertyMetadata(false, RoundedChanged));

    private static void RoundedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        bool value = (bool)e.NewValue;
        MyBox thisMyBox = (MyBox)sender;

        // Hide/show the edges
        thisMyBox.edgeRounded.Visibility = (value ? Visibility.Visible : Visibility.Hidden);
        thisMyBox.edgePolygon.Visibility = (value ? Visibility.Hidden : Visibility.Visible);
    }
}

但是,目标是拥有设计时属性,这样我就可以拥有:并且可以看到边缘根据设计属性而改变。上面的模型可以继承INotifyPropertyChanged以及UserControl,这样您就可以实现propertychanged事件,而不是dependencyproperty。目标是拥有设计时属性,尽管如此,我可以有:并看到边缘根据设计属性发生变化。上面的模型可以继承INotifyPropertyChanged以及UserControl,这样您就可以实现propertychanged事件,而不是dependencyproperty。我通过添加一个已更改的事件处理程序,然后更改dependencyproperty来实现这一点y引用它的属性注册:public static readonly dependencProperty RoundedProperty=dependencProperty.Register(“舍入”、typeof(bool)、typeof(MyBox)、new PropertyMetadata(false、RoundedChanged));我通过添加一个已更改的事件处理程序,然后更改依赖属性注册以引用它来实现这一点:public static readonly dependencProperty RoundedProperty=dependencProperty.Register(“Rounded”、typeof(bool)、typeof(MyBox)、new PropertyMetadata(false、RoundedChanged));