Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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# Can';I don’我不能让我的财产像我期望的那样运转_C#_Wpf_Xaml - Fatal编程技术网

C# Can';I don’我不能让我的财产像我期望的那样运转

C# Can';I don’我不能让我的财产像我期望的那样运转,c#,wpf,xaml,C#,Wpf,Xaml,我有一个非常简单的用户控件,它显示一个等待的动画,上面有一个文本: <UserControl x:Class="VNegoceNET.Controls.PleaseWait" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

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

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

我遗漏了什么?

WPF没有为DP(公共字符串文本)使用公共属性包装器,当从xaml(
)设置属性时,它直接使用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;
}

您可以绑定
TextBlock的
TextProperty
,而不是像前面提到的那样使用
PropertyChangedCallback

...
<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}}}"/>
...
。。。
...

你刚刚救了我的命!谢谢
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;
}
...
<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}}}"/>
...