I build up a Office 2010 Word Add In, added a Windows Forms Element to it and there in an ElementHost object containing my WPF.
That's the problem: I got the problem, that common properties (DP's) defined in the Xaml code and set in the ViewModel implementation (see code below) are not updated, even if I can't find any wrong code while debugging. This applies only to the properties I got, furthermore i got some ObservableCollection<> List's which are updated without any problems. Got anybody an idea? I red that I may define the properties by myself within the Code using something like this:
public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource",typeof(string),typeof(usercontrol1));
But this does not work too (I just added this line for example into the code-behind file of the Xaml file - and left anything else at it is)
My code looks this way:
Constructor of the Forms Element (it's actually private because I implemented it as Singleton):
private Sidebar()
{
InitializeComponent();
this.DoubleBuffered = true;
_wpfHost = new ElementHost();
_wpfHost.Dock = DockStyle.Fill;
_wpfUserControl = new WPFUI();
_wpfHost.Child = _wpfUserControl;
this.Controls.Add(_wpfHost);
}
Furthermore I created an underlying ViewModel and set this as DataContext Property of the WPFUI like this (it's also a Singleton implementation because I wanted to access in a further implementation the same instance from several places, but this does not come into the game at this time):
public WPFUI()
{
InitializeComponent();
this.DataContext = myViewModel.GetInstance();
}
My ViewModel properties are defined and used this way:
public ObservableCollection<myListViewItem> PeopleEntityResultObjects { get; private set; }
private string _NumberOfPeople;
public string NumberOfPeople
{
get { return _NumberOfPeople; }
set { SetField(ref _NumberOfPeople, value, () => NumberOfPeople); }
}
And finally the Xaml looks like this:
<TabControl>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text=" People "/>
<TextBlock Text="{Binding Path=NumberOfPeople}"/>
</StackPanel>
</TabItem.Header>
<ScrollViewer x:Name="PeopleListScrollViewer">
<ListView Name="PeopleListView" ItemsSource="{Binding Path=PeopleEntityResultObjects, Mode=OneWay}" IsSynchronizedWithCurrentItem="True">
.
.
.
</ListView>
</ScrollViewer>
</TabItem>
</TabControl>
Why is my ObservableCollection<> List updating but not the bound properties like
<TextBlock Text="{Binding Path=NumberOfPeople}"/>
? Can anybody guess, or i'm missing some basic property definition? (in a wpf application it is working that way).
EDIT
SetField() implementation:
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
{
if (selectorExpression == null)
throw new ArgumentNullException("selectorExpression");
MemberExpression body = selectorExpression.Body as MemberExpression;
if (body == null)
throw new ArgumentException("The body must be a member expression");
OnPropertyChanged(body.Member.Name);
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, Expression<Func<T>> selectorExpression)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(selectorExpression);
return true;
}
Regards, Thomas
1) Check your output window for binding errors. (Although they look ok to me)
2) My immediate thought would be that PropertyChanged
events are not being raised properly by your ViewModel. Can you show the implementation of SetField()
? I assume it's some wrapper for removing the magic strings inherent in INotifyPropertyChanged
?