一个数据绑定会阻止其他数据绑定工作

One databinding prevents other from working

我有一个 'status label' 的文本块。我希望这个标签被更新,当发生这种情况时,我希望它的颜色也被自动设置 - 以及可见性(标签在有内容之前不可见)。

问题是,如果我指定的不仅仅是文本绑定,那么文本块就不会改变(即文本不会出现,它仍然是隐藏的)。 实际上,我也尝试过不绑定可见性,而且前景似乎也阻止了绑定。

<TextBlock x:Name="StatusInfo"
                               HorizontalAlignment="Center" VerticalAlignment="Bottom"
                               FontSize="9"
                               Visibility="{Binding ElementName=ThisUc,
                                                    Path=StatusLabelVisibility}"
                               Text="{Binding ElementName=ThisUc,
                                              Path=StatusLabel}"
                               Foreground="{Binding ElementName=ThisUc,
                                                    Path=StatusLabelBrush}" />

这一切都在 UserControl 中,所以我使用 StatusLabel 属性 的依赖属性,因为我想将它绑定到主 window 中的属性...前景和可见性属性不是依赖属性,因为我不想公开它们。 这是我的 属性 setter 和 getter:

public string StatusLabel
        {
            get { return (string)GetValue(StatusLabelProperty); }
            set
            {
                SetValue(StatusLabelProperty, value);
                RaisePropertyChanged("StatusLabel");
                if (value != string.Empty)
                {
                    StatusLabelVisibility = System.Windows.Visibility.Visible;
                    if (value.HasAny("success", "ok") && !value.HasAny("partial"))
                    {
                        StatusLabelBrush = Brushes.Green;
                    }
                    else if (value.HasAny("fail"))
                    {
                        StatusLabelBrush = Brushes.DarkRed;
                    }
                    else if (value.HasAny("partial"))
                    {
                        StatusLabelBrush = Brushes.DarkGoldenrod;
                    }
                    else
                    {
                        StatusLabelBrush = Brushes.Black;
                    }
                }
                else
                {
                    StatusLabelVisibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

请告诉我我做错了什么,也许这不是完全正确的方法?

干杯

====================

虽然 Meredith 的回答解决了问题,但让我 post 发表评论以供将来参考(因为这对我来说并不明显):

这里是 - 如果您直接分配 UserControl 属性,而不是通过 属性 绑定,它似乎会丢失 'bound' - 如果您尝试更改绑定 属性,它不会像之前那样更新控件 'lost the bound'

干杯

如果 StatusLabel 是 DependencyProperty,则不能在 setter 中放置任何其他内容 - 它不会被正确调用。查找为 DependencyProperties 更改事件的方法。您需要一个 PropertyChangedCallback。查看 How to use PropertyChangedCallBack。提出你的道具变化,并在回调中设置所有其他属性。