【问题标题】:UserControl Dependency Property design timeUserControl 依赖属性设计时间
【发布时间】:2013-08-12 01:59:18
【问题描述】:

我正在 WPF 中创建一个简单的用户控件,其中包含一个按钮内的 TextBlock。

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

还有“Text”依赖属性。

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));

}

然后我像这样使用 UserControl:

<MyButton Text="Test" />

问题在于 Visual Studio 的设计没有改变,但它在运行时工作。

怎么了?

我也试过

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

在 UC 定义中,没有成功。

【问题讨论】:

    标签: c# wpf binding dependency-properties


    【解决方案1】:

    尝试使用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 关于FrameworkPropertyMetadataOptions.AffectsRender

    渲染或布局组合的某些方面(除了测量或 安排)受此依赖属性值更改的影响。

    对于其他情况,还有 AffectsMeasure、AffectsArrange 等选项。

    【讨论】:

    • 太棒了!起初,这似乎不起作用,所以我关闭并打开 Visual Studio,它现在工作正常。非常感谢。
    • 还得重启VS2017。好提示!
    【解决方案2】:

    金铲子候选人,我还是遇到了同样的问题,并在https://www.codeproject.com/Questions/1096567/How-to-set-a-custom-dependency-property-of-user-co的启发下解决了这个问题

    长话短说:您的依赖属性是在 UserControl 本身上设置的,您正试图将它的子属性绑定到它。孩子的绑定需要定义RelativeSource,因此TextBlock 应该如下所示:

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

    唯一需要的DataContext 赋值是您在构造函数后面的代码中已经拥有的赋值。


    更新

    但是后来我尝试了您的尝试并得出结论,如果您已经在 XAML 中定义了DataContext,则无需在每个控件中都提供它。这意味着您需要通过以下方式定义您的 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>
    

    像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 1970-01-01
      • 2014-08-31
      • 2017-12-24
      • 1970-01-01
      • 2023-04-07
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多