Playing with ComboBox.
Today I was playing little with ComboBox, and I made some binding, so here is my XAML:
<Window x:Class="TemplateChooser.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewModel="clr-namespace:TemplateChooser.ViewModel" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.Resources> <viewModel:TemplateChooserViewModel x:Key="TemplateChooserViewModel" /> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="30"/> </Grid.RowDefinitions> <ComboBox Name="myCombo" DataContext="{StaticResource TemplateChooserViewModel}" ItemsSource="{Binding TemplateItems}" SelectedValue="{Binding TemplateItems[0]}"/> <Button Grid.Row="1" Content="{Binding Path=SelectedValue, ElementName=myCombo}"/> </Grid> </Window>
Here notice line:
SelectedValue="{Binding TemplateItems[0]}"
After that, notice how I binded button
<Button Grid.Row="1" Content="{Binding Path=SelectedValue, ElementName=myCombo}"/>
I am not sure if this binding is proper, but it is working :)
Here is my view model:
public class TemplateChooserViewModel { public IEnumerable<string> TemplateItems { get; set; } public TemplateChooserViewModel() { TemplateItems = new[] { "Template1", "Template2" }; } }
Example project you can download from here.