Всем привет.
Попробовал у окна сделать статполе и к нему забиндиться. И что то не получается.
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region PropertyChanges
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private static System.ComponentModel.PropertyChangedEventHandler StaticPropertyChanged;
static protected void OnStaticPropertyChanged([CallerMemberName] String propertyName = "")
{
System.ComponentModel.PropertyChangedEventArgs e = new System.ComponentModel.PropertyChangedEventArgs(propertyName);
System.ComponentModel.PropertyChangedEventHandler h = StaticPropertyChanged;
if (h != null)
h(null, e);
}
void StaticNotifyPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// map the static name to the instance name
NotifyPropertyChanged(e.PropertyName);
}
#endregion
private static double _progress = 0.0;
public static double Progress
{
get
{
return _progress;
}
set
{
_progress = value;
OnStaticPropertyChanged();
}
}
}
XAML
/// Вариант 1 тут стаковерфлоу в конструкторе
<Window.Resources>
<ObjectDataProvider x:Key="ProgressManager" ObjectType="{x:Type local:MainWindow}" MethodName="Progress"/>
</Window.Resources>
<SomeElement
Value="{Binding Source={StaticResource ProgressManager}}" />
/// Вариант 2 так не биндится
<SomeElement
Value="{Binding Mode=OneWay, Source={x:Static local:MainWindow.Progress}}"/>
Проблему решил не статичным депенданси полем. Но вопрос вообще возможно реализовать привязку к статичному полю именно класса основного окна?
There are 10 types of people in the world: Those who understand binary, and those who don't.
There are 10 types of people in the world: Those who understand binary, and those who don't.
Здравствуйте, About Blank, Вы писали:
AB>
AB> public partial class MainWindow : Window, INotifyPropertyChanged
AB> {
AB> #region PropertyChanges
AB> public event PropertyChangedEventHandler PropertyChanged;
AB> // This method is called by the Set accessor of each property.
AB> // The CallerMemberName attribute that is applied to the optional propertyName
AB> // parameter causes the property name of the caller to be substituted as an argument.
AB> private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
AB> {
AB> if (PropertyChanged != null)
AB> {
AB> PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
AB> }
AB> }
AB> private static System.ComponentModel.PropertyChangedEventHandler StaticPropertyChanged;
AB> static protected void OnStaticPropertyChanged([CallerMemberName] String propertyName = "")
AB> {
AB> System.ComponentModel.PropertyChangedEventArgs e = new System.ComponentModel.PropertyChangedEventArgs(propertyName);
AB> System.ComponentModel.PropertyChangedEventHandler h = StaticPropertyChanged;
AB> if (h != null)
AB> h(null, e);
AB> }
AB> void StaticNotifyPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
AB> {
AB> // map the static name to the instance name
AB> NotifyPropertyChanged(e.PropertyName);
AB> }
AB> #endregion
AB> private static double _progress = 0.0;
AB> public static double Progress
AB> {
AB> get
AB> {
AB> return _progress;
AB> }
AB> set
AB> {
AB> _progress = value;
AB> OnStaticPropertyChanged();
AB> }
AB> }
AB>}
AB>
еще конструктор забыл
public MainWindow()
{
InitializeComponent();
...
StaticPropertyChanged += StaticNotifyPropertyChanged;
...
}
There are 10 types of people in the world: Those who understand binary, and those who don't.