I have a MVVM app. In main WPF window I have a combox and a textblock among other controls.
When I select a value from the combobox, the textblock text should change its text value dynamically according to the item selected in the combobox (depending on the id of the selected item in combobox).
My problem is that when I select a item in the combobox, textblock text does not change, it always have the default value. Any ideas how to solve this?
I want to do this using xaml only.
Model:
public class Item
{
#region Constructors
public Item() { }
public Item(int id, string desc)
{
this.Id = id;
this.Desc = desc;
}
#endregion
#region Properties
public int Id
{
get;
set;
}
public string Desc
{
get;
set;
}
#endregion
public override string ToString()
{
return this.Desc;
}
}
MVVM Property in view model:
private ObservableCollection<Item> _myItems;
public ObservableCollection<Item> MyItems
{
get { return _myItems; }
set { _myItems= value; }
}
View:
<ComboBox x:Name="MyWPFCombo"
ItemsSource="{Binding MyItems}"/>
<TextBlock Padding="5 10 0 0">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="Select the items:" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyWPFCombo, Path=Id}" Value="10">
<Setter Property="Text" Value="Select the old items:" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>