Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:在设计时从另一个属性值设置属性值_C#_Wpf - Fatal编程技术网

C# WPF:在设计时从另一个属性值设置属性值

C# WPF:在设计时从另一个属性值设置属性值,c#,wpf,C#,Wpf,我已经从标签创建了一个自定义控件。我添加了一个新的依赖属性“MyCaption”。我将内容项目设置为隐藏: [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public new string Content { get; set; } 现在,我想要这种行为:如果我设置MyCaption值,我希望内容在设计时也设置该值 一些想法 编辑:我试图像这样定义依赖项属性,但它不起作用: public static reado

我已经从标签创建了一个自定义控件。我添加了一个新的依赖属性“MyCaption”。我将内容项目设置为隐藏:

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new string Content { get; set; }
现在,我想要这种行为:如果我设置MyCaption值,我希望内容在设计时也设置该值

一些想法

编辑:我试图像这样定义依赖项属性,但它不起作用:

public static readonly DependencyProperty MyCaptionProperty =
    DependencyProperty.Register("MyCaption ",
                                typeof(string),
                                typeof(TMSLabel));

    [Description("Valore della label"), Category("MyProperties")] 
    public string MyCaption 
    {
        get { return (string)GetValue(MyCaptionProperty); }
        set { 
            SetValue(MyCaptionProperty, value);
            Content = value;
        }
    }
提示:如果我删除了使che Content属性隐藏的代码,上面的代码就可以工作了

来自-

重要提示:不要向这些属性添加任何逻辑,因为只有在从代码设置属性时才会调用它们。如果从XAML设置属性,将直接调用SetValue()方法

因此,试图从Dependency属性设置中设置Content属性听起来并不正确

试着改用PropertyChangedCallBack怎么样

public class MyLabel : Label
{
    public static readonly DependencyProperty MyCaptionProperty =
        DependencyProperty.Register("MyCaption ",
                                    typeof(string),
                                    typeof(MyLabel), new FrameworkPropertyMetadata(string.Empty, OnChangedCallback));

    public string MyCaption
    {
        get { return (string)GetValue(MyCaptionProperty); }
        set { SetValue(MyCaptionProperty, value); }
    }

    private static void OnChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var labelControl = d as Label;
        if (labelControl != null)
            labelControl.Content = e.NewValue;
    }
}

<Window x:Class="WpfTestProj.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfTestProj="clr-namespace:WpfTestProj"        
    Title="MainWindow" Height="350" Width="525">
<wpfTestProj:MyLabel MyCaption="This is a test" />
公共类MyLabel:标签
{
公共静态只读从属属性MyCaptionProperty=
DependencyProperty.Register(“MyCaption”,
类型(字符串),
typeof(MyLabel),新的FrameworkPropertyMetadata(string.Empty,OnChangedCallback));
公共字符串MyCaption
{
get{return(string)GetValue(MyCaptionProperty);}
set{SetValue(MyCaptionProperty,value);}
}
私有静态void OnChangedCallback(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var labelControl=d作为标签;
如果(labelControl!=null)
labelControl.Content=e.NewValue;
}
}

Isidor您在寻找什么?我不需要在设计或运行时有不同的行为。在运行时我可以很容易地使用绑定,但在设计时我不能。这是您的意图吗,您的自定义控件试图隐藏Label content属性?通过隐藏原始“内容”属性,您试图做什么?公共新字符串内容{get;set;}我的目的是在Properties视图中隐藏Content属性。Oops。。。我下面的答案可能对你没有帮助。需要指出的一点是,只有在代码隐藏时,MyCaption属性才起作用。如果从xmal分配MyCaption值,则分配将不起作用。希望能有帮助。