뒤에있는 코드 (App.xaml.cs)에서 App.xaml로 변수를 바인딩하는 가장 좋은 방법은 무엇일까? 그럼 필자는 필요할 경우 응용 프로그램의 어느 곳에서나이 리소스를 참조 할 수 있습니다. .WPF 응용 프로그램 리소스에 날짜 시간 바인딩
이 클래스를 찾아보십시오. 응용 프로그램 자원을 [icode] NotifyingDateTime [/ icode]에 바인드 할 수 있어야하며 해당 클래스가 변경되면 업데이트됩니다.
public class NotifyingDateTime : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private DateTime _now;
public NotifyingDateTime()
{
_now = DateTime.Now;
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
public DateTime Now
{
get { return _now; }
private set
{
_now = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Now"));
}
}
void timer_Tick(object sender, EventArgs e)
{
Now = DateTime.Now;
}
}
그래도 난 바인딩하는 방법을 알아낼 수 없으며하여 DispatcherTimer는 응용 프로그램이 시작될 때를 시작할 수 있도록 클래스를 실행합니다 바인딩 경우도 이해가 안 돼요.
희망적입니다.
이것은 코드 숨김이 아니며 ViewModel입니다. 뷰 모델의 인스턴스를 만들고 뷰 (wpf 페이지) DataContext에 적용합니다. –
무슨 뜻입니까 : "app.xaml에 바인딩"? App.xaml은 UI 요소 정의가 아닌 응용 프로그램 propertys의 방어 파일입니다 – JPVenson