Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Nested Views in WPF MVVM
    primarykey
    data
    text
    <p>Hi I am trying to understand the best practise to use nested views.</p> <p>I have an 'Attribute' View which is bound to collection of name value pairs in the view model. I need to reuse this at various places in my UI.</p> <p>I have another 'Condition View' which has a string property and Dictionary(string,string) collection. I tried to create a text box and add the Attribute View control in the XAML</p> <pre><code> &lt;StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Label Content="{x:Static l:StringResources.ConditionViewLabelText}" /&gt; &lt;TextBox Text="{Binding Type,Mode=TwoWay}" Width="100" /&gt; &lt;/StackPanel&gt; &lt;vw:AttributeView /&gt; &lt;/StackPanel&gt; </code></pre> <p>I want to bind the AttributeView's bound property to the Dictionary(string,string)'s collection property of the parent view model. Whats the best way to do it. I am not able to bind the vw:AttributeView to a ConditionViewModels ?</p> <p>Can you please let me know the best practise to do this?</p> <p>-- EDIT Please find my AttributeView (This is the child view's xaml code). The data template is bound to an observable collection on the AttributeViewModel</p> <pre><code> &lt;StackPanel&gt; &lt;ItemsControl ItemsSource="{Binding AllAttributes}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBox Width="100" Text="{Binding Name, Mode=TwoWay}" Margin="5,0,5,0" /&gt; &lt;TextBox Width="100" Text="{Binding Value, Mode=TwoWay}" Margin="5,0,5,0" /&gt; &lt;TextBlock Width="100" Text="{Binding KeyValue, Mode=OneWay}" /&gt; &lt;Button Width="50" Content="{x:Static l:StringResources.AttributeViewButtonDeleteText}" Command="{Binding Path=DataContext.DeleteAttribute, ElementName=AttributeControl}" CommandParameter="{Binding Name}"&gt;&lt;/Button&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;Button Name="btnSomething" Content="{x:Static l:StringResources.AttributeViewButtonAddText}" Command="{Binding AddNewAttribute}" /&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre>
    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.
 

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