Note that there are some explanatory texts on larger screens.

plurals
  1. PODataTemplate Binding for ListBox to ViewModel property
    text
    copied!<p>I'm using a ListBox with a DataTemplate.</p> <pre><code> &lt;ListBox Grid.Row="1" Grid.ColumnSpan="3" Grid.RowSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Order.OrderLines}" &gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Grid.Column="0"&gt;Qty&lt;/TextBlock&gt; &lt;TextBox Text="{Binding LineQty, Mode=TwoWay}" Grid.Column="1" /&gt; &lt;TextBlock Grid.Row="1" Grid.Column="0"&gt;Weight&lt;/TextBlock&gt; &lt;TextBox Text="{Binding Path=LineWeight}" Grid.Row="1" Grid.Column="1" /&gt; &lt;TextBlock Grid.Column="0" Grid.Row="2"&gt;Pallet Weights&lt;/TextBlock&gt; &lt;TextBox Text="{Binding PalletWeights}" Grid.Row="2" Grid.Column="1" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>The TextBox value is binding properly. The problem is I have a property on my ViewModel called "ViewMode" which I have the IsEnabled property of the TextBox bound to an App.xaml Style DataTrigger:</p> <pre><code> &lt;Style TargetType="{x:Type TextBox}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding ViewMode}" Value="Add"&gt; &lt;Setter Property="BorderBrush" Value="White"&gt;&lt;/Setter&gt; &lt;Setter Property="BorderThickness" Value="2,2,0,0"&gt;&lt;/Setter&gt; &lt;Setter Property="BorderBrush" Value="Black"&gt;&lt;/Setter&gt; &lt;Setter Property="BorderThickness" Value="0,0,2,2"&gt;&lt;/Setter&gt; &lt;Setter Property="Background" Value="LightBlue"&gt;&lt;/Setter&gt; &lt;Setter Property="Foreground" Value="Black"&gt;&lt;/Setter&gt; &lt;Setter Property="FontWeight" Value="Bold"&gt;&lt;/Setter&gt; &lt;Setter Property="IsEnabled" Value="true"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding ViewMode}" Value="Edit"&gt; &lt;Setter Property="BorderBrush" Value="White"&gt;&lt;/Setter&gt; &lt;Setter Property="BorderThickness" Value="2,2,0,0"&gt;&lt;/Setter&gt; &lt;Setter Property="BorderBrush" Value="Black"&gt;&lt;/Setter&gt; &lt;Setter Property="BorderThickness" Value="0,0,2,2"&gt;&lt;/Setter&gt; &lt;Setter Property="Background" Value="LightBlue"&gt;&lt;/Setter&gt; &lt;Setter Property="Foreground" Value="Black"&gt;&lt;/Setter&gt; &lt;Setter Property="FontWeight" Value="Bold"&gt;&lt;/Setter&gt; &lt;Setter Property="IsEnabled" Value="true"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding ViewMode}" Value="View"&gt; &lt;Setter Property="IsEnabled" Value="false"&gt;&lt;/Setter&gt; &lt;Setter Property="Foreground" Value="Gray"&gt;&lt;/Setter&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;Setter Property="Margin" Value="2" /&gt; &lt;/Style&gt; </code></pre> <p>This works for all of my other TextBoxes. How do I get the IsEnabled property to work from within the DataTemplate? The DataContext of the ListBox is referencing the ViewModel property "Order", so I would think it should be able to see the ViewModel property "ViewMode".</p> <p>Thanks, -Sid.</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