【问题标题】:Can't make my DependencyProperty work as I expected [duplicate]无法使我的 DependencyProperty 按预期工作[重复]
【发布时间】:2018-11-30 08:07:06
【问题描述】:

我有一个非常简单的用户控件,它在上面显示一个等待动画:

<UserControl x:Class="VNegoceNET.Controls.PleaseWait"
             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:VNegoceNET.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid x:Name="RootElement" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Grid.RowSpan="3" Background="White" Content="" Opacity="0.8"/>
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"
                   Grid.Row="0" FontSize="18" Foreground="Black"
                   Margin="8" x:Name="Caption" Text="Loading..."/>
        <local:SpinningWait Grid.Row="1"/>
    </Grid>
</UserControl>

我想这样使用它:

<controls:PleaseWait Text="Jegg Robot"/>

我的问题是它仍然显示“正在加载...”而不是“Jegg Robot”,尽管我的依赖属性:

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

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text", typeof(String), typeof(PleaseWait), new PropertyMetadata("Loading in progress..."));

    public string Text
    {
        get => (string)this.GetValue(TextProperty);
        set
        {
            Caption.Text = value;
            this.SetValue(TextProperty, value);
        }
    }
}

我错过了什么?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    WPF 不对 DP(公共字符串文本)使用通用属性包装器,当从 xaml (&lt;controls:PleaseWait Text="Jegg Robot"/&gt;) 设置属性时,它直接使用 SetValue()。因此不会调用 setter 中的代码。

    需要的是propertyChangedCallback:

    public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", typeof(String), typeof(PleaseWait), 
                                new PropertyMetadata("Loading in progress...", OnTextChanged));
    
    public string Text
    {
        get => (string)this.GetValue(TextProperty);
        set { this.SetValue(TextProperty, value); }
    }
    
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    {
        var c = (PleaseWait) d;
        c.Caption.Text = c.Text;
    }
    

    【讨论】:

    • 你刚刚救了我的命!谢谢。
    【解决方案2】:

    您可以绑定TextBlockTextProperty,而不是使用PropertyChangedCallback,例如提到的ASh

    ...
    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"
        Grid.Row="0" FontSize="18" Foreground="Black"
        Margin="8" x:Name="Caption" Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PleaseWait}}}"/>
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 1970-01-01
      • 2020-04-23
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多