我有一個自定義用戶控件,其中的文本有時會更改文本塊。 的代碼的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