Note that there are some explanatory texts on larger screens.

plurals
  1. POPass view to viewmodel with datatemplate
    text
    copied!<p>I have a window named <code>ParameterEditorView</code> with a <code>ParameterEditorViewModel</code> as <code>DataContext</code>. In the <code>ParameterEditorViewModel</code> I have a list of <code>ParameterViewModel</code>. In the <code>ParameterEditorView</code> I have an <code>ItemsControl</code> whose <code>ItemsSource</code> is binded to the list of <code>ParameterViewModel</code> in the <code>ParameterEditorViewModel</code>. I need the <code>ParameterViewModel</code> to have a reference to the ParameterView (more on that later). In the <code>Resources</code> section of the <code>ParameterEditorView</code> I add the <code>DataTemplate</code>:</p> <pre><code>&lt;DataTemplate DataType="{x:Type my:ParameterViewModel}" &gt; &lt;my:ParameterView HorizontalAlignment="Left"/&gt; &lt;/DataTemplate&gt; </code></pre> <p>So, how can I pass a reference of the <code>ParameterView</code> that is created to show the <code>ParameterViewModel</code> to it? </p> <p>The reason I need the <code>ParameterView</code> in the <code>ParameterViewModel</code> is the following: I have a <code>TextBox</code> whose <code>Text</code> property is binded to the <code>PropertyModelView.Name</code> property. But I want to display a default string when the <code>Name</code> is empty or Null. I've tried to set the property value to the default string I want when that happens but the <code>TextBox.Text</code> is not set in this scenario. I do something like this:</p> <pre><code>private string _name; public string Name { get { return _name; } set { if (value == null || value.Length == 0) Name = _defaultName; else _name = value; } } </code></pre> <p>I've also tried to specifically set the <code>TextBox.Text</code> binding mode to TwoWay without success. I think this is a defense mechanism to prevent an infinite loop from happening but I don't know for sure. Any help on this front would also be highly appreciated.</p> <p>Thanks, José Tavares</p>
 

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