I am really fairly new to WPF and have just wrapped my head around MVVM / Expression.Blend.
I am trying to use the Combobox "SelectedIndexChanged" event with InvokeCommandAction, binding to a command specified in the ViewModel.
However, the code in the command is never executed (the value myvalue
is never updated).
What am I doing wrong here?
XAML:
<UserControl x:Class="GDX.UI.Views.AnalyseView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i1="http://schemas.microsoft.com/xaml/behaviors"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:GDX.UI.Views"
mc:Ignorable="d"
Height="720" Width="1210">
<Control.Resources>
<ResourceDictionary Source="pack://application:,,,/GDX.UI;component/ResourceDictionary/ResourceDictionary.xaml"/>
</Control.Resources>
<materialDesign:Card Margin="15,15,15,15" Grid.Column ="0" Grid.Row="0" Grid.RowSpan="6" Grid.ColumnSpan="2">
<ScrollViewer Grid.Row="0" Grid.RowSpan="5" Grid.Column="0" Grid.ColumnSpan="2" VerticalScrollBarVisibility="Hidden">
<ItemsControl ItemsSource="{Binding buildingComponents}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="10">
<TextBlock Margin="10" Text="{Binding Path=UIName}" FontWeight="Bold"/>
<ComboBox
ItemsSource="{Binding Path=ConstructionOptions}"
SelectedItem="{Binding Path=Construction}"
HorizontalAlignment="Center"
Style="{StaticResource MaterialDesignOutlinedComboBox}"
Width="256"
Height="50"
BorderBrush="Red"
materialDesign:HintAssist.Hint="Baukonstruktion">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedIndexChanged">
<i:InvokeCommandAction Command="{Binding SetBuildingComponentConstruction}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</materialDesign:Card>
ViewModel:
using GDX.IO.Schemas;
using GDX.IO.Statics;
using GDX.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace GDX.ViewModel.ViewModels
{
public class AnalyseViewModel : BaseViewModel
{
public ObservableCollection<BuildingComponent> buildingComponents { get; set; }
public double myvalue { get; set; }
public ICommand SetBuildingComponentConstruction { get; set; }
public AnalyseViewModel()
{
this.buildingComponents = new ObservableCollection<BuildingComponent>();
if (Building.BuildingComponents.Count != 0) //if contains elements
{
foreach (BuildingComponent bcomp in Building.BuildingComponents)
{
this.buildingComponents.Add(bcomp);
}
}
this.SetBuildingComponentConstruction = new RelayCommand(this.setBuildingComponentConstruction);
this.myvalue = 0;
}
public void setBuildingComponentConstruction()
{
myvalue = 1;
}
}
}
CodePudding user response:
Looks like the SetBuildingComponentConstruction method is defined on AnalyseViewModel, but where you are binding in the itemstemplate the datacontext would be a BuildingComponent. You may have to "walk" up to the datacontext of your UserControl.
How do I use WPF bindings with RelativeSource?
Looking at the XAML Binding Failures tab in VS while debugging may also provide you with some direction.
Try
Command="{Binding DataContext.SetBuildingComponentConstruction,
RelativeSource={RelativeSource AncestorType=ComboBox}}"