C# WPF为什么初始绑定不触发setter?

C# WPF为什么初始绑定不触发setter?,c#,wpf,data-binding,C#,Wpf,Data Binding,我有下面的按钮 因此,原始绑定似乎不会在代码隐藏中触发Setter。为什么不呢?如果旧的xaml只是简单的,那么它当然不会工作。按钮文本和内部文本块文本之间没有任何绑定。UpdownButton文本的初始绑定不应该隐式调用myUpdownButton.Text=newString;,哪个更新了文本块?如果旧的xaml是您在上面评论的内容,我认为即使在加载后也无法正常工作。您显式设置了myUpdownButton.Text=newString,我认为您应该仔细检查并重新确认它。当然,当前的XAML

我有下面的按钮


因此,原始绑定似乎不会在代码隐藏中触发Setter。为什么不呢?

如果旧的xaml只是简单的,那么它当然不会工作。按钮文本和内部文本块文本之间没有任何绑定。UpdownButton文本的初始绑定不应该隐式调用myUpdownButton.Text=newString;,哪个更新了文本块?如果旧的xaml是您在上面评论的内容,我认为即使在加载后也无法正常工作。您显式设置了myUpdownButton.Text=newString,我认为您应该仔细检查并重新确认它。当然,当前的XAML应该可以工作。看起来我理解你的意思,如果使用XAML和codebehind中的所有注释代码,你当然可以在codebehind中设置文本,XAML绑定将绕过这个numCopyTB.Text=Text;但是在codebehind中设置属性是行不通的,所以它是有效的。这只是设计上的问题,您可能想问XAML设计者为什么会这样。在msdn关于DependencyProperty的页面中也会注意到这一点,请尝试阅读本文。最后,我们可以知道它是如何实现的,所以遵循它,而不是问为什么。
<Button x:Class="MvvmAttempt.UpdownButton"
         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:MvvmAttempt"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" HorizontalContentAlignment="Stretch">
    <Button.Resources>
        <ResourceDictionary Source="resources/Styles.xaml"/>
    </Button.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Viewbox Stretch="Uniform" Grid.Column="0">
            <TextBlock x:Name="numCopyTB" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=Text}"/>
            <!--<TextBlock x:Name="numCopyTB"/>-->   <-- doesn't work propery
        </Viewbox>
        <Viewbox Grid.Column="1">
            <TextBlock Style="{StaticResource updownBlock}"/>
        </Viewbox>

    </Grid>
</Button>
public partial class UpdownButton : Button 
{

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(UpdownButton));


    public UpdownButton()
    {
        InitializeComponent();
    }

    public string Text
    {
        get { return GetValue(TextProperty) as string; }
        set { SetValue(TextProperty, value);
              //numCopyTB.Text = Text; <-- doesn't work properly
        }
    }
}
<local:UpdownButton Text="{Binding Path=Copies, Mode=TwoWay}"/>