简体   繁体   English

UserControl 依赖属性设计时间

[英]UserControl Dependency Property design time

I'm creating a simple User Control in WPF that contains a TextBlock inside a Button.我正在 WPF 中创建一个简单的用户控件,其中包含一个按钮内的 TextBlock。

<UserControl x:Class="WpfExpansion.MyButton"..... >
    <Grid >
        <Button Background="Transparent" >
            <TextBlock Text="{Binding Path=Text}"/>
        </Button>
    </Grid>
</UserControl>

And also the "Text" dependency property.还有“文本”依赖属性。

public partial class MyButton : UserControl
{
    public MyButton()
    {
        InitializeComponent();
        this.DataContext = this;         
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(MyButton), new PropertyMetadata(string.Empty));

}

And then I use the UserControl like this:然后我像这样使用 UserControl:

<MyButton Text="Test" />

The problem is that the Visual Studio design does not change, but it works in runtime.问题是 Visual Studio 设计没有改变,但它在运行时工作。

What is wrong?怎么了?

I also tried我也试过

DataContext="{Binding RelativeSource={RelativeSource Self}}"

Inside the UC definition, without success.在UC定义里面,没有成功。

Try using FrameworkPropertyMetadata instead of PropertyMetadata , specifying AffectsRender like below, then restart Visual Studio:尝试使用FrameworkPropertyMetadata而不是PropertyMetadata ,指定AffectsRender如下,然后重新启动Visual Studio:

public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(MyButton),
        new FrameworkPropertyMetadata(string.Empty,
            FrameworkPropertyMetadataOptions.AffectsRender));

MSDN Documentation about FrameworkPropertyMetadataOptions.AffectsRender says关于FrameworkPropertyMetadataOptions.AffectsRender MSDN 文档

Some aspect of rendering or layout composition (other than measure or arrange) is affected by value changes to this dependency property.渲染或布局组合的某些方面(度量或排列除外)受此依赖属性的值更改的影响。

For other cases, there are options like AffectsMeasure, AffectsArrange, etc.对于其他情况,还有 AffectsMeasure、AffectsArrange 等选项。

Golden shovel candidate, still I came across the same problem and had it solved being inspired by https://www.codeproject.com/Questions/1096567/How-to-set-a-custom-dependency-property-of-user-co金铲候选人,我仍然遇到了同样的问题,并在https://www.codeproject.com/Questions/1096567/How-to-set-a-custom-dependency-property-of-user-的启发下解决了它合作

Long story short: your dependency property is set on the UserControl itself and you are trying to bind the it's child property to it.长话短说:您的依赖属性设置在UserControl本身上,您正试图将其子属性绑定到它。 The child's binding needs to have RelativeSource defined, hence the TextBlock should look like this:子绑定需要定义RelativeSource ,因此TextBlock应如下所示:

            <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=Text}" />

The only DataContext assignment needed is the one you already have in the code behind in the constructor.唯一需要的DataContext分配是您在构造函数后面的代码中已有的分配。


UPDATE更新

But then I tried your attempts and came to the conclusion that if you define the DataContext in XAML already, you don't need to provide it in each of the controls. 但是后来我尝试了您的尝试并得出结论,如果您已经在 XAML 中定义了DataContext ,则不需要在每个控件中都提供它。 This means you need to define your UC the following way ( d:DataContext=... does the trick): 这意味着您需要通过以下方式定义您的 UC( d:DataContext=...可以解决问题):

 <UserControl x:Class="WpfExpansion.MyButton" 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:YRS100_Data_Analysis" mc:Ignorable="d" d:DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid> <Button Background="Transparent"> <TextBlock Text="{Binding Path=Text}" /> </Button> </Grid> </UserControl>

Works like a charm.像魅力一样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM