Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF DependencyProperty not updating in WPF UserControl (ElementHost)
    primarykey
    data
    text
    <p>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. </p> <p>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&lt;> 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:</p> <pre><code>public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource",typeof(string),typeof(usercontrol1)); </code></pre> <p>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)</p> <p>My code looks this way:</p> <p>Constructor of the Forms Element (it's actually private because I implemented it as Singleton):</p> <pre><code>private Sidebar() { InitializeComponent(); this.DoubleBuffered = true; _wpfHost = new ElementHost(); _wpfHost.Dock = DockStyle.Fill; _wpfUserControl = new WPFUI(); _wpfHost.Child = _wpfUserControl; this.Controls.Add(_wpfHost); } </code></pre> <p>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):</p> <pre><code>public WPFUI() { InitializeComponent(); this.DataContext = myViewModel.GetInstance(); } </code></pre> <p>My ViewModel properties are defined and used this way:</p> <pre><code>public ObservableCollection&lt;myListViewItem&gt; PeopleEntityResultObjects { get; private set; } private string _NumberOfPeople; public string NumberOfPeople { get { return _NumberOfPeople; } set { SetField(ref _NumberOfPeople, value, () =&gt; NumberOfPeople); } } </code></pre> <p>And finally the Xaml looks like this:</p> <pre><code>&lt;TabControl&gt; &lt;TabItem&gt; &lt;TabItem.Header&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text=" People "/&gt; &lt;TextBlock Text="{Binding Path=NumberOfPeople}"/&gt; &lt;/StackPanel&gt; &lt;/TabItem.Header&gt; &lt;ScrollViewer x:Name="PeopleListScrollViewer"&gt; &lt;ListView Name="PeopleListView" ItemsSource="{Binding Path=PeopleEntityResultObjects, Mode=OneWay}" IsSynchronizedWithCurrentItem="True"&gt; . . . &lt;/ListView&gt; &lt;/ScrollViewer&gt; &lt;/TabItem&gt; &lt;/TabControl&gt; </code></pre> <p>Why is my ObservableCollection&lt;> List updating but not the bound properties like </p> <pre><code>&lt;TextBlock Text="{Binding Path=NumberOfPeople}"/&gt; </code></pre> <p>? Can anybody guess, or i'm missing some basic property definition? (in a wpf application it is working that way).</p> <p><em>EDIT</em></p> <p>SetField() implementation:</p> <pre><code>protected virtual void OnPropertyChanged&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; 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&lt;T&gt;(ref T field, T value, Expression&lt;Func&lt;T&gt;&gt; selectorExpression) { if (EqualityComparer&lt;T&gt;.Default.Equals(field, value)) return false; field = value; OnPropertyChanged(selectorExpression); return true; } </code></pre> <p>Regards, Thomas</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload