我有一个自定义用户控件,其中的文本有时会更改文本块。 的代码的TextBlocks是自定义用户控件TextBlock.text绑定
XAML:
<TextBlock Text="{Binding ElementName=dashboardcounter, Path=Counter}" FontFamily="{Binding ElementName=dashboardcounter, Path=FontFamily}" HorizontalAlignment="Left" Margin="17,5,0,0" VerticalAlignment="Top" FontSize="32" Foreground="#FF5C636C"/>
的.cs:
private static readonly DependencyProperty CounterProperty = DependencyProperty.Register("Counter", typeof(string), typeof(DashboardCounter));
public string Counter
{
get { return (string)GetValue(CounterProperty); }
set { SetValue(CounterProperty, value); }
}
我的班级:
private string _errorsCount;
public string ErrorsCount
{
get { return _errorsCount; }
set { _errorsCount = value; NotifyPropertyChanged("ErrorsCount"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
绑定的用户控件说:
dashboardCounter.Counter = view.ErrorsCount;
TextBlock显示 - 绝对没有。
我做错了什么? 字符串是动态的,有时会发生变化。 它最初的诠释,但我选择了它是字符串,而不是和转换,而不是创建的IValueConverter
难道说的ElementName值是区分大小写和一个C少校在失踪你的绑定:“ElementName = dashboardcounter”? – AirL
x:Name =“dashboardcounter” - 那不是它 – VisualBean
它工作,如果我设置“计数器”目录dashboardCounter.Counter =“sometext”; – VisualBean